10

I use a Python virtual environment. Basically, it works fine, but I run into problems when compiling some Python bindings, namely with libIGL and pybind11.

CMake has the following Python-related variables:

 PYTHON_EXECUTABLE                /users/me/libs/pyvenv/bin/python                           
 PYTHON_INCLUDE_DIR               /usr/include/python2.7                                          
 PYTHON_LIBRARY                   /usr/lib64/libpython2.7.so 

It seems that it can detect the executable which is a python3.5 of the previously-activated virtual environment properly, but it finds some wrong 2.7 paths for include and library.

So I'd like to just set those paths manually to my virtual environment. I browsed around in the directory structure of the virtual environment, and I think I found the includes in /users/me/libs/pyvenv/include/python3.5m. But I can't find the libpython*, there's no *.so file at all in my virtual environment. So which library should I use in that case?

Michael
  • 7,407
  • 8
  • 41
  • 84
  • Obviously, you should use the correct library! I believe the library you would be looking for should be the same as above, replacing `python2.7` with `python3.5`. By the way, it would be nice to have a reproducible example. And could you explain a bit more about "CMake has the following Python-related variables"? (Are these environment variables, set in a script...etc) – Adonis Aug 18 '17 at 11:35
  • @Adonis: As far as I understand, the question is about existing projects (libIGL and pybind11) which use [find_package(PythonLibs)](https://cmake.org/cmake/help/v3.9/module/FindPythonLibs.html) for detect Python libraries. So the code is unlikely needed here. – Tsyvarev Aug 18 '17 at 12:18
  • 1
    Tsyvarev, yes, it would be nice if CMake would find the files automatically. But the question can be understood even more basic, just as: "Where do I find "libpython" in a virtual environment." – Michael Aug 18 '17 at 14:52
  • I am not expert with virtual environment in python, but I **guess** the python library **doesn't depend** from the environment. Environment determines site packages as python scripts or libraries, but basic python library remains the same. So, probably you need to search the library file in system location, like `/usr/lib64/`. – Tsyvarev Aug 18 '17 at 15:11
  • @Tsyvarev: While your proposed method solved the problem, I still find it very unsatisfying. The purpose of a virtual environment is to provide access to a Python installation without caring about the underlying system. So directly accessing the library in system-specific paths kind of violates this principle. Just my 2 cents ;-) – Michael Aug 19 '17 at 13:04
  • You may treat python library as part of "underlying system". So placing it outside of environment directory violates nothing. – Tsyvarev Aug 19 '17 at 14:49
  • Yes, but that means that a Python virtual environment does not provide/interface to a full Python installation. Unless there's good reason, it seems very arbitrary or even bug-ish to not provide a link/alias to the library, while includes and binaries are provided. – Michael Aug 19 '17 at 17:12
  • @Michael Hello, what did you end up doing for "PYTHON_LIBRARY"? I'm facing the same problem. – aysljc Mar 05 '20 at 14:53

2 Answers2

0

Restrict python libs to match version of found interpreter in cmake:

find_package(PythonInterp REQUIRED)
find_package(PythonLibs "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}" REQUIRED )

Or use FindPython if cmake>=3.12 is availble

Sergei
  • 1,599
  • 12
  • 21
0

Similarly to this question, you could run python from within your virtualenv and run

import pybind11
print(pybind11.__file__)
# '/home/me/.pyenv/versions/py36/lib/python3.6/site-packages/pybind11/__init__.py'

Christian
  • 1,162
  • 12
  • 21