[BUG] module not find when run from the same folder as the shared object
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Issue description
I'm compiling a Python module, and running a Python code from the same folder as the shared object. This mostly works, but sometimes fails with no apparent reason with
ModuleNotFoundError: No module named 'mymodule'
Reproducible example code
I put a minimal reproducible example with CI in a clean environment here. As can be observed, it fails in this case on Windows. Just to list the example here as well:
main.cpp
#include <vector>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
namespace py = pybind11;
template <class T>
inline T foo(T& arg)
{
T ret = arg;
for (auto& i : ret) {
i *= 2;
}
return ret;
}
PYBIND11_MODULE(mymodule, m)
{
m.doc() = "Module description";
m.def("foo", &foo<std::vector<int>>, "Function description", py::arg("arg"));
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.18..3.20)
project(mymodule)
find_package(Python REQUIRED COMPONENTS Interpreter Development NumPy)
find_package(pybind11 REQUIRED CONFIG)
pybind11_add_module(mymodule main.cpp)
target_link_libraries(mymodule PUBLIC pybind11::module Python::NumPy)
target_compile_definitions(mymodule PRIVATE VERSION_INFO=0.1.0)
example.py
import mymodule
import numpy as np
A = (np.random.random([100]) * 100).astype(np.uint64)
assert np.all(A * 2 == mymodule.foo(A))
Build instructions
cmake -Bbuild -DPython_EXECUTABLE=`which python`
cd build
cmake --build .
cp ../example.py .
python example.py
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the minimal example in main.cpp, CMakeLists.txt, and example.py, then follow the listed CMake build instructions in a clean Windows environment. Compare the failing import behavior with other environments and inspect the linked reproduction for additional details. Done means the reported same-folder import failure is understood and a verified fix or project-level resolution is documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cmake, cpp, python
- Domain
- backend, build-system
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100