Skip to content

[BUG] module not find when run from the same folder as the shared object #3025

Description

@tdegeus

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions