1

Following this thread

Assume I have the following files in the directory prog:

main.cpp (located in the directory prog)

int main(int argc, char *argv[]){


Py_Initialize();

//Adding current path
PyObject* sys = PyImport_ImportModule("sys");
PyObject* path = PyObject_GetAttrString(sys, "path");
PyObject* cur_dir = PyUnicode_FromString("");
PyList_Append(path, cur_dir);

PyObject* myModuleString = PyUnicode_DecodeFSDefault((char*)"multiset_bell_numbers.py");
PyObject *pModule =  PyImport_Import(myModuleString);
if (pModule == nullptr) {
    PyErr_Print();
    throw std::runtime_error("pModule is empty");
}

return 0;
}

multiset_bell_numbers.py (located in the directory prog)

from sympy.utilities.iterables import multiset_partitions



def calc_partitions(arr):
    res = list(multiset_partitions(arr))
    res.sort(key=lambda part:len(part))
    return res

CMakeLists.txt

cmake_minimum_required(VERSION 3.24)
project(prog)

set(CMAKE_CXX_STANDARD 17)

find_package(Python REQUIRED COMPONENTS Interpreter Development)
include_directories(${Python_INCLUDE_DIRS})
add_executable(prog main.cpp)
target_link_libraries(prog ${Python_LIBRARIES})

But still got an error:

ModuleNotFoundError: No module named 'multiset_bell_numbers'

I have also tried to remove ".py" from the suffix of the string What should I do?

linuxbeginner
  • 39
  • 1
  • 7
  • Remember that `import` looks in the current working directory. It's not the directory where the executable is. If you have done `cd prog` and `main`, then it will work. – Tim Roberts Feb 18 '23 at 18:37
  • I made the executable in the same directory, but it's still doesn't work – linuxbeginner Feb 18 '23 at 18:40
  • 1
    What about if you pass just the module name without the *.py* extension? Also the *C* source file location is irrelevant. – CristiFati Feb 18 '23 at 18:42
  • It doesn't matter where the executable is. What matters is the WORKING DIRECTORY. That's what Python searches. – Tim Roberts Feb 18 '23 at 19:04

1 Answers1

0

In order for Python to find the multiset_bell_numbers module, it needs to be located in a directory that is in the Python Module Search Path.

The following directories will be checked:

  • The current folder from which the program executes.
  • A list of folders specified in the PYTHONPATH environment variable, if you set it before.
  • An installation-dependent list of folders that you configured when you installed Python.

If you ensure that the multiset_bell_numbers is in the same directory that the program is executed from, it should import successfully.

If that is not feasible, you can try updating PYTHONPATH to include the directory containing your module.

Increasingly Idiotic
  • 5,700
  • 5
  • 35
  • 73
  • It's kind of weird that the current path is not being searched for the `multiset_bell_numbers.py`. It's still not working if I extract the py. It's still not working if I made sure that the path is inside python search (I have added the code that ensures that, same problem) I have added the CMakeLists.txt for future reference of the problem (In case I would solve this..) – linuxbeginner Feb 19 '23 at 19:31