I have a problem with a little program Im making
This is my program:
#define PY_SSIZE_T_CLEAN
#include <Python.h>
int main() {
Py_SetPythonHome(L"C:/Users/fxct/AppData/Local/Python/pythoncore-3.12-64");
// Py_SetPath(L"C:/Users/fxct/AppData/Local/Python/pythoncore-3.12-64/Lib;"
// L"C:/Users/fxct/AppData/Local/Python/pythoncore-3.12-64/Lib/site-packages");
Py_Initialize();
PyRun_SimpleString("import sys; print(sys.path)");
PyRun_SimpleString("import numpy; print('numpy loaded successfully')");
PyRun_SimpleString("import cv2; print('cv2 loaded successfully')");
Py_Finalize();
return 0;
}
I want to embed python in c++, right now I got it running. I can use the standard python library but when I try to import opencv it gives me this error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\fxct\AppData\Local\Python\pythoncore-3.12-64\Lib\site-packages\cv2\__init__.py", line 181, in <module>
bootstrap()
File "C:\Users\fxct\AppData\Local\Python\pythoncore-3.12-64\Lib\site-packages\cv2\__init__.py", line 153, in bootstra
p
native_module = importlib.import_module("cv2")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\fxct\AppData\Local\Python\pythoncore-3.12-64\Lib\importlib\__init__.py", line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: DLL load failed while importing cv2: The specified module could not be found.
I installed opencv-python, opencv-contrib-python and opencv-contrib-python-headless in my system wide python as I saw in another similar forum that installing one of the last two solved the problem but it's not working for me
The first two python strings run correctly but it crashes when I want to import cv2, I dont really know what to do
CMakeLists in CLion:
cmake_minimum_required(VERSION 4.0)
project(python_embed_test)
set(CMAKE_CXX_STANDARD 20)
set(Python_EXECUTABLE "C:/Users/fxct/AppData/Local/Python/pythoncore-3.12-64/python.exe" CACHE FILEPATH "" FORCE)
set(Python_LIBRARY "C:/Users/fxct/AppData/Local/Python/pythoncore-3.12-64/libs/python312.lib" CACHE FILEPATH "" FORCE)
set(Python_INCLUDE_DIR "C:/Users/fxct/AppData/Local/Python/pythoncore-3.12-64/include" CACHE PATH "" FORCE)
find_package(Python 3.12 COMPONENTS Development REQUIRED)
add_executable(python_embed_test main.cpp)
target_link_libraries(python_embed_test PRIVATE Python::Python)
... set(Python_EXECUTABLE "path" CACHE FILEPATH "" FORCE) set(Python_LIBRARY "path" CACHE FILEPATH "" FORCE) set(Python_INCLUDE_DIR "path" CACHE PATH "" FORCE) find_package(Python 3.12 COMPONENTS Development REQUIRED) ...I had to force the locations because CMake was looking for python in my toolchain (vcpkg) maybe that is a problem ("path" is the correct in CMakeLists)