I'm new to CMake and have trouble understanding some usage concepts.
I'm calling a python script from a c++ program:
#include <Python.h>
...
Py_Initialize();
PyRun_SimpleFile(...);
Py_Finalize();
The corresponding cmake entries in my cmake file are:
FIND_PACKAGE(PythonLibs REQUIRED)
...
TARGET_LINK_LIBRARIES(MyApplication ${PYTHON_LIBRARIES})
This works as long as my python script isn't using any modules installed into the site-packages directory, otherwise I get an ImportError. This question shows how to find the location of the site-packages directory with CMake, but what should I tell CMake to do with it?
EDIT: Problem solved. Turns out FIND_PACKAGE(PythonLibs) finds a different python installation from what I'm normally using (/usr/local/lib/libpython2.7.dylib instead of /Library/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib - I'm on mac), which is how I get standard python modules, but none that I installed myself. To change the PYTHONPATH back to normal, I added
try:
import some_package
except ImportError:
if "my_python_path" in sys.path: raise
sys.path.append("my_python_path")
at the top of my python script.
You are effectively embedding python in your program when you do this. Did you call Py_Initialize() before PyRun_SimpleFile ? Have a look at Embedding Python in Another Application.
Py_Initialize() will set up sys.path and is required to set the python environment.
If you can find out where python is installed, it is possible to set python home to override python path calculations. Use Py_SetPythonHome() before Py_Initialize().
On posix type OSes, here is a comment in getpath.c (cpython implementation of path resolving):
The best way to solve the problem that the wrong version is found (for instance 3.0 instead of 2.7) is to specify the minimum version to find_package (this will choose any version >= 2.7):
or to get the exact version:
You can setup manually on cmake libs
\usr\share\cmake-3.2.3\Modules\FindPythonLibs.cmake
:You can tell cmake where to find this PythonLibs by specifying the path to your python libraries like this:
This will then set the ${PYTHON_LIBRARIES} inside cmake to the right path.
To find out which other possible options (besides PYTHON_LIBRARIES) you can give to cmake (with the -DARG option) try running
Then press
c
to configure, andt
for advanced options.For example, you might also want to set