0

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)
5
  • Please provide enough code so others can better understand or reproduce the problem. Commented Nov 12 at 5:00
  • 2
    Python's API is for C. you can keep the C++ tag (for all I care) but expect the C++ regulars to be less forgiving than the C regulars. Commented Nov 12 at 11:42
  • 2
    looks to me like you haven't installed the opencv package correctly. what did you do? Commented Nov 12 at 11:43
  • @ChristophRackwitz I installed opencv-python, opencv-contrib-python and opencv-contrib-python-headless in my system wide python but none of them worked. I'm using CLion and I did this in my CMakeLists: ... 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) Commented Nov 12 at 13:53
  • and there's an issue... you installed more than one package of opencv. that usually causes some issues somewhere. remove them all, install exactly one. -- what are you talking about, system-wide? what exactly is the environment you're running this in? Commented Nov 12 at 19:12

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.