Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyImport_ImportModule
#1
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
Larz60+ write Jan-20-2024, 10:10 AM:
you can still use BBCode tags with C++. (modified for this instance)
Reply
#2
I don't think that C++ questions belong here, but you may get an answer, regardless.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020