Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import on Linux #94

Open
jcnelson30 opened this issue Jan 21, 2025 · 1 comment
Open

Import on Linux #94

jcnelson30 opened this issue Jan 21, 2025 · 1 comment

Comments

@jcnelson30
Copy link

jcnelson30 commented Jan 21, 2025

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.

@tbenthompson
Copy link
Owner

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants