How to get Python.h in Header Search Paths
已回答
New user so please have patience. Trying to add Python.h to a project so I can call a python script from a C++ program.
I added a virtual environment and the python interpreter (Mac OS)..
Header search paths are:

But I can't find a way to add the path for Python.h. It is located in:
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers
How can I add this? I tried adding this to CMakeLists.txt:
include_directories(${CMAKE_CURRENT_LIST_DIR}//Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers)
THANKS!
Joe Kelly
请先登录再写评论。
Hello!
Please take a look at this thread, for example. You don't need to add a Python interpreter in CLion (CLion | Settings | Build, Execution, Deployment | Python Interpreter). However, you need to add the following lines to your CMakeLists.txt file
and then do Tools | CMake | Reset Cache and Reload Project.
THANKS so much. It almost got me there. By including those 3 lines & resetting, my project was able to find <Python.h>. However, when I tried to run it, I got a "library not loaded error".
So, I did add the python interpreter. Then under External Libraries, “Python 3.9 (ASP) interpreter library” shows up. [Note, this is an a Mac with ARM]. Reset again but same error.
/Users/joe/projects/ASP/cmake-build-debug/ASP
dyld[37003]: Library not loaded: @rpath/Python3.framework/Versions/3.9/Python3
Referenced from: <03F5F8ED-3A2E-3F50-B94D-1DFBD520F3D5> /Users/joe/projects/ASP/cmake-build-debug/ASP
Reason: tried: '/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/Python3.framework/Versions/3.9/Python3' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/Python3.framework/Versions/3.9/Python3' (no such file), '/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/Python3.framework/Versions/3.9/Python3' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/Python3.framework/Versions/3.9/Python3' (no such file)
Process finished with exit code 134 (interrupted by signal 6:SIGABRT)
Do I need to add a package? If so, what is the correct name. There are tons of python packages that show up in Settings/Build…/Python Interpreter/ but none are clearly the library itself.
Thanks again,
Joe
For me (CLion 2025.1.1, macOS with ARM, Python installed from Homebrew), running the application works fine without adding a Python interpreter in CLion (CLion | Settings | Build, Execution, Deployment | Python Interpreter).
<name of your target>
in thetarget_link_libraries()
command to the appropriate name of the target?message("${Python3_LIBRARIES}")
to your CMakeLists.txt, do Tools | CMake | Reset Cache and Reload Project, and provide the entire output from the CMake tool window.Thanks again.
1) Yes, I did change the target name. Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.31)
project(ASP)
set(CMAKE_CXX_STANDARD 23)
add_executable(ASP main.cpp)
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
include_directories(${Python3_INCLUDE_DIRS})
target_link_libraries(ASP ${Python3_LIBRARIES})
2) Here is the output from the CMake tool window
/Applications/CLion.app/Contents/bin/cmake/mac/aarch64/bin/cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_MAKE_PROGRAM=/Applications/CLion.app/Contents/bin/ninja/mac/aarch64/ninja -G Ninja -S /Users/joe/projects/ASP -B /Users/joe/projects/ASP/cmake-build-debug
-- The C compiler identification is AppleClang 17.0.0.17000013
-- The CXX compiler identification is AppleClang 17.0.0.17000013
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Python3: /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/bin/python3.9 (found version "3.9.6") found components: Interpreter Development Development.Module Development.Embed
/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/libpython3.9.dylib
-- Configuring done (0.4s)
-- Generating done (0.0s)
-- Build files have been written to: /Users/joe/projects/ASP/cmake-build-debug
[Finished]
3) Notes: first I am using the version of Python3 that comes with the Xcode command line tools. I did not install another version. Second, the build seems to work. But when I try to run the app, I get the "library not loaded message: here:
/Users/joe/projects/ASP/cmake-build-debug/ASP
dyld[40245]: Library not loaded: @rpath/Python3.framework/Versions/3.9/Python3
Referenced from: <C5C4E8F1-B579-3FC5-9734-9ACB49668D7D> /Users/joe/projects/ASP/cmake-build-debug/ASP
Reason: tried: '/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/Python3.framework/Versions/3.9/Python3' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/Python3.framework/Versions/3.9/Python3' (no such file), '/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/Python3.framework/Versions/3.9/Python3' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/Python3.framework/Versions/3.9/Python3' (no such file)
Process finished with exit code 134 (interrupted by signal 6:SIGABRT)
Again, thanks so much for your help.
Joe
Are you able to build and run this exact CMake project in the terminal? If it doesn't work in the terminal either, it means that the issue is related not to CLion but to CMake and libraries from Xcode. You can try, for example, using this workaround as suggested here. Or you can try installing Python from Homebrew.
First, I never thought this was a CLion problem. Just my problem in learning how to use it. Your help has been great!
I did try and run it from the terminal. Essentially the same error message:
ASP % ./cmake-build-debug/ASP
dyld[40462]: Library not loaded: @rpath/Python3.framework/Versions/3.9/Python3
Referenced from: <C5C4E8F1-B579-3FC5-9734-9ACB49668D7D> /Users/joe/projects/ASP/cmake-build-debug/ASP
Reason: tried: '/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/Python3.framework/Versions/3.9/Python3' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/Python3.framework/Versions/3.9/Python3' (no such file), '/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/Python3.framework/Versions/3.9/Python3' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/Python3.framework/Versions/3.9/Python3' (no such file)
zsh: abort ./cmake-build-debug/ASP
The dynamic library is located at:
/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/libpython3.9.dylib
I (naively) tried copying & pasting those 2 lines from the link you sent me, here is updated CMakeLists.txt:
cmake_minimum_required(VERSION 3.31)
project(ASP)
set(CMAKE_CXX_STANDARD 23)
add_executable(ASP main.cpp)
# Add rpath entries for Xcode frameworks.
set(CMAKE_BUILD_RPATH "$ENV{DEVELOPER_DIR}/Library/Frameworks" CACHE STRING "")
set(CMAKE_INSTALL_RPATH "$ENV{DEVELOPER_DIR}/Library/Frameworks" CACHE STRING "")
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
include_directories(${Python3_INCLUDE_DIRS})
target_link_libraries(ASP ${Python3_LIBRARIES})
message("${Python3_LIBRARIES}")
Do I need to (somehow) define “DEVELOPER_DIR”? It doesn't appear to be set. Can that be done in the CMakeLists file?
I tried this:
ASP % env DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer cmake /Users/joe/projects/ASP
env: cmake: No such file or directory
If there isn't something simple I can do to fix this, the next step will be installing Python from Homebrew.
Thanks again,
Joe
I'm afraid such questions are out of the scope of CLion support and my area of expertise.
I'd assume
DEVELOPER_DIR
is/Applications/Xcode.app/Contents/Developer
. You can try substituting$ENV{DEVELOPER_DIR}
with just/Applications/Xcode.app/Contents/Developer
in your CMakeLists.txt and doing Tools | CMake | Reset Cache and Reload Project.If it doesn't help, please refer to CMake-related forums, Stack Overflow, etc.
I understand completely and am just grateful you got me this far. I'll continue trying to figure this out and let you know if I find a solution.
Thanks again,
Joe
Problem solved! You put me on the right path with the advice about rpath. What I needed to do was put those statements before I defined the target. So, a CMake mistake on my part. Thanks again for your help.