tbenthompson / cppimport

Import C++ files directly from Python!
MIT License
1.18k stars 67 forks source link

Fail to run `python -m cppimport build` #69

Closed VungleTienan closed 2 years ago

VungleTienan commented 2 years ago

Hi there~ When try to run %sh python -m cppimport build in databricks, it returns: No module named cppimport.__main__; 'cppimport' is a package and cannot be directly executed

What the reason could be? Thanks very much!

cppimport==21.3.7 Python==3.8.10

VungleTienan commented 2 years ago

I also tried this:

import cppimport
foobar = cppimport.imp_from_filepath("/tmp/somecode.cpp")

And got the error below:

ModuleNotFoundError                       Traceback (most recent call last)
<command-3823353284205349> in <module>
      1 import cppimport
      2 import cppimport.import_hook
----> 3 foobar = cppimport.imp_from_filepath("/tmp/somecode.cpp")

/databricks/python/lib/python3.8/site-packages/cppimport/__init__.py in imp_from_filepath(filepath, fullname)
     64     if not is_build_needed(module_data) or not try_load(module_data):
     65         template_and_build(filepath, module_data)
---> 66         load_module(module_data)
     67     return module_data["module"]
     68 

/databricks/python/lib/python3.8/site-packages/cppimport/importer.py in load_module(module_data)
     58         new_flags = old_flags | cppimport.settings["rtld_flags"]
     59         sys.setdlopenflags(new_flags)
---> 60         _actually_load_module(module_data)
     61         sys.setdlopenflags(old_flags)
     62     else:

/databricks/python/lib/python3.8/site-packages/cppimport/importer.py in _actually_load_module(module_data)
     45 
     46 def _actually_load_module(module_data):
---> 47     module_data["module"] = importlib.import_module(module_data["fullname"])
     48 
     49 

/usr/lib/python3.8/importlib/__init__.py in import_module(name, package)
    125                 break
    126             level += 1
--> 127     return _bootstrap._gcd_import(name[level:], package, level)
    128 
    129 

/usr/lib/python3.8/importlib/_bootstrap.py in _gcd_import(name, package, level)

/usr/lib/python3.8/importlib/_bootstrap.py in _find_and_load(name, import_)

/usr/lib/python3.8/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

ModuleNotFoundError: No module named 'somecode'
VungleTienan commented 2 years ago

The file somecode.cpp contains

// cppimport
#include <pybind11/pybind11.h>

namespace py = pybind11;

int square(int x) {
    return x * x;
}

PYBIND11_MODULE(somecode, m) {
    m.def("square", &square);
}
/*
<%
setup_pybind11(cfg)
%>
*/
mityax commented 2 years ago

I think the python -m cppimport build failure is because there hasn't been a new release to PyPi since the feature was added so you're likely to have a version that just doesn't support it yet.

mityax commented 2 years ago

You could try installing from github directly to get the current version:

$ python -m pip install git+https://github.com/tbenthompson/cppimport.git
VungleTienan commented 2 years ago

python -m pip install git+https://github.com/tbenthompson/cppimport.git

Thanks @mityax , but still get the same error~

mityax commented 2 years ago

Hm weird. Maybe you could try with --force-reinstall

VungleTienan commented 2 years ago

Hm weird. Maybe you could try with --force-reinstall

Still failed, So is there an output path contains .so files?

manish-kumar-iisc commented 2 years ago

I have also same problem No module named cppimport.main; 'cppimport' is a package and cannot be directly executed

PS: pip install cppimport==20.8.4.2 import cppimport somecode = cppimport.imp("somecode") #This will pause for a moment to compile the module somecode.square(9)

Its working, when i used above one

mityax commented 2 years ago

Well, turns out --force-reinstall still uses the locally cached version:

$ pip install --force-reinstall cppimport
Collecting cppimport
  Using cached cppimport-21.3.7.tar.gz (1.7 MB) #  <--- here
  [...]

I used

$ pip install --force-reinstall --no-cache git+https://github.com/tbenthompson/cppimport.git

and it worked:

$ python -m cppimport --help
usage: cppimport [-h] [--verbose] [--quiet] {build} ...
  [...]
mityax commented 2 years ago

@tbenthompson I think an update to PyPi would be good to avoid this confusion and the need to install from git. Or is there anything to wait for before publishing a new version? :)

tbenthompson commented 2 years ago

Yes, I'll push an update to PyPi this week! Thanks for reminding me.

tbenthompson commented 2 years ago

I just pushed a new release. Please let me know if there are any issues!

VungleTienan commented 2 years ago

Thanks everyone~