简体   繁体   中英

When running python script from CMake installed python modules can't be found

Local environment

$ uname -a
Darwin Mac-mini.local 20.6.0 Darwin Kernel Version 20.6.0: Wed Jun 23 00:26:31 PDT 2021; root:xnu-7195.141.2~5/RELEASE_X86_64 x86_64
$ python3 --version
Python 3.9.7
$ cmake --version
cmake version 3.21.2

CMake suite maintained and supported by Kitware (kitware.com/cmake).
$ xcode-select --version
xcode-select version 2384.

Xcode: Version 12.5.1 (12E507)

Problem

I have some large which build is managed by cmake .

Now some dependencies are downloaded by python script. paramiko library is needed since some of this dependencies are search and download using ssh/sftp connections. This details are not important.

Now when I run from command line this script it works perfectly. It finds all python modules and it is able to download all dependencies.
Problem appears when same script is invoked from cmake command in CMakeLists.txt . Script fails since it can't load paramiko library what is weird since script works if run directly from same command line as cmake.. -G Xcode .

Basically it looks like this:

add_custom_target(DependenciesDownload ALL ./download.py <some arguments>
    BYPRODUCTS
        "${CMAKE_CURRENT_BINARY_DIR}/poroduct/libproduct.${PRODUCT_EXTENSION}"
    WORKING_DIRECTORY "${PROJECT_SRC_ROOT}/scriptPath"
    COMMENT "Unzips dependency from SMB network drive or sftp address"
)

I also tried version where python is invoked explicitly: python3./download.py <some arguments> and env python3./download.py <some arguments> with exactly same outcome.

Note

I had same issue on other MacOS machine and it started to work properly I have no idea what I've done to make it work and I can't replicate that now.

I had a similar problem on Linux. When a Python script was invoked from CMake it couldn't find any packages.

The problem was the PATH environment variable was not identical. The directory where the Python packages was installed was missing when the script was invoked from CMake.

I don't use Mac and not sure if this works in the same way. But you can try:

  • Print out the PATH variable in the python script. And check if there is a difference.
import os 
print(os.environ) # printing environment variables
  • Set the absolute path to the package in the script. (for troubleshooting)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM