Python Forum

Full Version: PyImport_ImportModule
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This C++-code is not able to import numy:

#include <iostream>
#include <filesystem>
//#include "C:\Program Files\Python311\include\Python.h"
#include <Python.h>

#include "C:\Program Files\Python311\Lib\site-packages\numpy\core\include\numpy\arrayobject.h"

int main()
{
  
	PyObject* pPythonModule = NULL;
	Py_Initialize();
	//import_array1(0);
	PyRun_SimpleString("import os, sys;sys.path.append(os.getcwd())\n");
	PyObject* pName = PyUnicode_DecodeFSDefault("numpy");
	pPythonModule = PyImport_Import(pName);
    PyErr_Print();
}
The output is:
Output:
Could not find platform dependent libraries <exec_prefix> Traceback (most recent call last): File "C:\Program Files\Python311\Lib\site-packages\numpy\core\__init__.py", line 24, in <module> from . import multiarray File "C:\Program Files\Python311\Lib\site-packages\numpy\core\multiarray.py", line 10, in <module> from . import overrides File "C:\Program Files\Python311\Lib\site-packages\numpy\core\overrides.py", line 8, in <module> from numpy.core._multiarray_umath import ( ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'
During handling of the above exception, another exception occurred:
Error:
Traceback (most recent call last): File "C:\Program Files\Python311\Lib\site-packages\numpy\__init__.py", line 144, in <module> from numpy.__config__ import show as show_config File "C:\Program Files\Python311\Lib\site-packages\numpy\__config__.py", line 4, in <module> from numpy.core._multiarray_umath import ( File "C:\Program Files\Python311\Lib\site-packages\numpy\core\__init__.py", line 50, in <module> raise ImportError(msg) ImportError: IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE! 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 Please note and check the following: * The Python version is: Python3.11 from "C:\Users\Thomas\source\repos\PythonImport\x64\Release\PythonImport.exe" * The NumPy version is: "1.26.3" and make sure that they are the versions you expect. Please carefully study the documentation linked above for further help. Original error was: No module named 'numpy.core._multiarray_umath' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Program Files\Python311\Lib\site-packages\numpy\__init__.py", line 149, in <module> raise ImportError(msg) from e ImportError: Error importing numpy: you should not try to import numpy from its source directory; please exit the numpy source tree, and relaunch your python interpreter from there. C:\Users\Thomas\source\repos\PythonImport\x64\Release\PythonImport.exe (Prozess "24700") wurde mit Code "0" beendet. Um die Konsole beim Beenden des Debuggens automatisch zu schließen, aktivieren Sie "Extras" > "Optionen" > "Debuggen" > "Konsole beim Beenden des Debuggings automatisch schließen". Drücken Sie eine beliebige Taste, um dieses Fenster zu schließen.
I am running this in VS 2022. I haven newly installed python 3.11 three times. I added any possible path to PYTHONPATH. Nothing helps.
Any ideas.

Thx. Thomas
I don't think that C++ questions belong here, but you may get an answer, regardless.
It’s not clear what your goal is.
https://python.readthedocs.io/en/latest/...dding.html

If you want to evaluate some simple expressions
Use 1.1. Very High Level Embedding

You can run PyRun_SimpleString in a while loop with cin, import numpy inside the simple string

If you actually want to transfer data between C++ and Python .
Use 1.3. Pure Embedding

If you plan on doing something meaningful, you should consider using boost::python, which has build in support for numpy