I am attempting to leverage some python libraries (NumPy, matplotlib) in C++ code. I'm using Windows, Visual Studio 2022, and cmake to build the project. The Python libraries are installed inside of a conda environment running Python 3.10. While I am seemingly able to build against the correct python version, the executable fails to launch the python interpreter.
In order to get Visual Studio to recognize the python environment, I activate the conda environment and then launch Visual Studio from command line start devenv
My CMakeLists.txt is configured
cmake_minimum_required (VERSION 3.20)
project(MyProject)
add_executable (${CMAKE_PROJECT_NAME} "MyProject.cpp" "MyProject.h"
find_package(Python3 REQUIRED COMPONENTS Development Interpreter NumPy REQUIRED)
include_directories(${Python3_INCLUDE_DIRS}
${Python3_NumPy_INCLUDE_DIRS}
)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE Python3::Python)
include(CMakePrintHelpers)
cmake_print_variables(Python3_EXECUTABLE)
cmake_print_variables(Python3_INCLUDE_DIRS)
The cmake_print_variables lines confirm that I am picking up the correct python environment
1> [CMake] -- Python3_EXECUTABLE="C:/Users/user/anaconda3/envs/myenv/python.exe"
1> [CMake] -- Python3_INCLUDE_DIRS="C:/Users/user/anaconda3/envs/myenv/include"
While the program compiles, attempting to run it fails as soon as a python function is called
Python path configuration:
PYTHONHOME = (not set)
PYTHONPATH = (not set)
program name = 'plotting'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = 'C:\\Users\\user\\source\\repos\\MyProject\\out\\build\\x64-release\\MyProject.exe'
sys.base_prefix = 'C:\\Users\\user\\anaconda3\\envs\\myenv'
sys.base_exec_prefix = 'C:\\Users\\user\\anaconda3\\envs\\myenv'
sys.platlibdir = 'lib'
sys.executable = 'C:\\Users\\user\\source\\repos\\MyProject\\out\\build\\x64-release\\MyProject.exe'
sys.prefix = 'C:\\Users\\user\\anaconda3\\envs\\myenv'
sys.exec_prefix = 'C:\\Users\\user\\anaconda3\\envs\\myenv'
sys.path = [
'C:\\Users\\user\\anaconda3\\envs\\myenv\\python310.zip',
'.\\DLLs',
'.\\lib',
'C:\\Users\\user\\source\\repos\\MyProject\\out\\build\\x64-release',
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00001ef8 (most recent call first):
<no Python frame>
Other queries (init_fs_encoding: failed to get the Python codec of the filesystem encoding & Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding, when trying to start uwsgi) indicate that this output is generated when PYTHONPATH and PYTHONHOME are incorrectly configured. I am currently not explicitly configuring those in any way (and besides, I would expect the build process to properly configure this anyway).
Attempting to set these parameters to my environment
(myenv) set PYTHONPATH=C:\Users\user\anaconda3\envs\myenv
(myenv) set PYTHONHOME=C:\Users\user\anaconda3\envs\myenv\Lib\site-packages
changes the behavior, but it also appears incorrect, yielding
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
The link above seems to be pertinent only to a pure python environment. In any case, it seems like in the scase, the environment isn't properly activated. I have confirmed that I can load NumPy when I launch python in my environment.
How do I make my c++ code actually link properly to the correct conda python environment?