module include path - /usr/local/lib/python3 vs /usr/local/lib/python3.9

Joachim Wuttke j.wuttke at fz-juelich.de
Thu Jun 17 03:53:09 EDT 2021


How to write a platform-independent CMake install command
to install a Swig-generated Python module?

The following brings us very close to a solution:
```
execute_process(
     COMMAND "${Python3_EXECUTABLE}"
     -c "from distutils import sysconfig as sc;
         print(sc.get_python_lib(prefix='', plat_specific=True))"
     OUTPUT_VARIABLE PYTHON_SITE
     OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "Python module libcerf will be installed to ${PYTHON_SITE}")

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/<our_module>.py
         DESTINATION ${PYTHON_SITE})
```

So far, we only tested under Debain, where the problem arises:

- distutils.sysconfig.get_python_lib(...) returns lib/python3/dist-packages
- CMake provides CMAKE_INSTALL_PREFIX=/usr/local/
- So installation goes to /usr/local/lib/python3/dist-packages
- sys.path, however, does not contain /usr/local/lib/python3/dist-packages.

sys.path contains /usr/local/lib/python3.9/dist-packages, and also
/usr/lib/python3/dist-packages, so this is slightly incoherent.
Anyway, we won't want to change sys.path. So we need to change
the above CMake/Python code to return a local installation directory
that is part of sys.path.

Thanks for any hints - Joachim




More information about the Python-list mailing list