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

ImportError due to pybind11 autodetecting python version in CMakeLists.txt #49

Closed
keckj opened this issue Feb 11, 2020 · 3 comments
Closed

Comments

@keckj
Copy link
Contributor

keckj commented Feb 11, 2020

Steps to reproduce:

  1. Have two or more different versions of python 3 installed (python3.5 and python3.6 for example):
sudo apt-get install python3.5-dev python3.6-dev
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3.5 get-pip.py
python3.6 get-pip.py
  1. Install pykeops using pip (or from sources):
pip3.5 install numpy && pip3.5 install pykeops
pip3.6 install numpy && pip3.6 install pykeops

This will install two different keops modules:
<module 'pykeops' from '/usr/local/lib/python3.5/dist-packages/pykeops/init.py'>
<module 'pykeops' from '/usr/local/lib/python3.6/dist-packages/pykeops/init.py'>

  1. Create python file test.py containing:
import pykeops
pykeops.clean_pykeops()          # just in case old build files are still present
pykeops.test_numpy_bindings()    # perform the compilation
  1. The test script will fail for at least one python version:
  • python3.5 test.py fails with ImportError: dynamic module does not define module export function (PyInit_libKeOpsnumpyb10acd1892)
  • python3.6 test.py succeeds with pyKeOps with numpy bindings is working!

What really happens:

This happens because pykeops/common/compile_routines.py calls cmake on pykeops/CMakeLists.txt which contains add_subdirectory(pybind11) which will detect python3.6 by default (in this specific case). This generates a shared library <CACHE_DIR>/pykeops-1.3-cpython-35/libKeOpsnumpyb10acd1892.so targeting python 3.6 instead of 3.5 and the importlib module fails to load the library.

How to fix:

The simplest solution I found is to enforce the python version directly in the build script by using the PYBIND11_PYTHON_VERSION variable. Adding set(PYBIND11_PYTHON_VERSION 3.5) at the beginning of /usr/local/lib/python3.5/dist-packages/pykeops/CMakeLists.txt fixes the problem.

I imagine this could be done automatically by detecting python version during build / before installation. This fix could solve many issues such as #2 #8 #28 #37 and others.

@bcharlier
Copy link
Member

bcharlier commented Mar 20, 2020

Hi @Poulpy21 ,

thank you for your valuable comment and sorry for the delay!

Could you give a try on your system to see if commit fe8bc76 does the trick ? It does on my system but, just in case...

thanks again,

b.

@bcharlier
Copy link
Member

I have included the changes in release v1.4. Fill free to reopen the issue if needed;

thanks again.

b.

@keckj
Copy link
Contributor Author

keckj commented Apr 5, 2020

Thanks for your patch ! I tried it by upgrading to version 1.4 directly with pip and it works like a charm :)

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