cppimport icon indicating copy to clipboard operation
cppimport copied to clipboard

Import on Linux

Open jcnelson30 opened this issue 10 months ago • 1 comments

Hello,

I recently got into PyBind11 as I needed somethings sped up. I develop on my OSX machine and then run long-running processes on my Ubuntu Server.

I'm currently using: pybind11 2.13.6 and cppimport 22.8.2

On OSX, I use the following line to import:

btf_cpp = cppimport.imp_from_filepath(f"{pathlib.Path(
    __file__).parent.resolve()}/cpp/btf.cpp")

Which has no problem finding, building, then importing the module. This 100% works for me on OSX.

The issue I run into, is when I run it on my linux box. I rsync the entire project directory (leaving out the built files). The app pauses during the initial rebuild but then fails import the module as seen below.

  File "/home/bt/tl/btf.py", line 15, in <module>
    btf_cpp = cppimport.imp_from_filepath(f"{pathlib.Path(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/miniconda3/envs/dev/lib/python3.12/site-packages/cppimport/__init__.py", line 88, in imp_from_filepath
    load_module(module_data)
  File "/home/miniconda3/envs/dev/lib/python3.12/site-packages/cppimport/importer.py", line 104, in load_module
    _actually_load_module(module_data)
  File "/home/miniconda3/envs/dev/lib/python3.12/site-packages/cppimport/importer.py", line 91, in _actually_load_module
    module_data["module"] = importlib.import_module(module_data["fullname"])
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/miniconda3/envs/dev/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'btf'
(dev) me@server:~/bt$ 

I can confirm the package gets built properly on the linux machine as it generates: btf.cpython-312-x86_64-linux-gnu.so next to my cpp file.

I guess I'm not sure how it ultimately registers the built module or how I should go about debugging this further.

Any help would be greatly appreciated. This is a great tool that made the development super convenient. The cpp project I built is only two files so not trying to roll out the cmake carpet for this.

jcnelson30 avatar Jan 21 '25 03:01 jcnelson30

Thanks for posting! Are you able to share the directory structure? It looks like there might be some bug in the module path resolution code when you use imp_from_filepath.

A workaround might be to do:

import sys
import cppimport
sys.path.append(f"{pathlib.Path(__file__).parent.resolve()}/cpp/")
cppimport.imp("btf")

If that workaround succeeds at solving the problem for you, can you let me know?

tbenthompson avatar Jan 21 '25 16:01 tbenthompson