Skip to content

'numpy.core._multiarray_umath' not found on embedded Python, but found on console. #13051

Closed

Description

@RichardEdye

I can import numpy in the Python console, but cannot import it in an embedded python C++ application that used the same Python version with the same folders. I have only this Python version on my PC. Importing other packages from the application works.

The application sets Py_SetPythonHome() to my Python folder:
C:\Users\MyName\AppData\Local\Programs\Python\Python37-32

print(os.file) prints the correct file.

Reproducing code example:

Py_SetProgramName(Py_DecodeLocale(ProgramName,0));
Py_SetPythonHome(Py_DecodeLocale("C:\\Users\\Richard\\AppData\\Local\\Programs\\Python\\Python37-32",0));
Py_Initialize();
PyModuleMain = PyImport_AddModule("__main__");
PyGlobalDict = PyModule_GetDict(PyModuleMain);
const char *Content = "import os; print(os.__file__)\nimport numpy";
PyRun_SimpleString(Content);
import os; print(os.__file__)
import numpy

Error message:

Traceback (most recent call last):
File "C:\Users\Richard\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy\core_init_.py", line 40, in
from . import multiarray
File "C:\Users\Richard\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy\core\multiarray.py", line 12, in
from . import overrides
File "C:\Users\Richard\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy\core\overrides.py", line 6, in
from numpy.core._multiarray_umath import (
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "", line 2, in
File "C:\Users\Richard\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy_init_.py", line 142, in
from . import core
File "C:\Users\Richard\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy\core_init_.py", line 71, in
raise ImportError(msg)
ImportError:
...

Numpy/Python version information:

1.16.2, 3.7 32 bit Windows

Activity

  1. added this to the milestone on Feb 27, 2019
  2. charris commented on Feb 27, 2019

    @charris
    Member

    This is probably a duplicate of several other issues but I think the embedded Python is new. See if the discussion at #12977 is helpful. How did you install NumPy? Are you working with Anaconda?

  3. RichardEdye commented on Feb 27, 2019

    @RichardEdye
    Author

    I have installed with pip install numpy. No Anaconda and no other Pythons on the system.

    I get the error message with the Python debug DLL. When I use the release DLL, the line "import numpy" immediately causes a crash in the PyRun_SimpleString() call.

  4. RichardEdye commented on Feb 27, 2019

    @RichardEdye
    Author

    I have also added ...Lib\site-packages\numpy\.libs to the PATH as suggested in the other thread, but to no avail. That folder contains a libopenblas.JKAMQ5EVHIVCPXP2XZJB2RQPIN47S32M.gfortran-win32.dll.

  5. mattip commented on Mar 6, 2019

    @mattip
    Member

    A couple of things might be happening.

    • Something is affecting the dll search order. This might be offset by copying the dll from'.lib to core next to the _multiarray_umath.pyd file.
    • Something in your embedded app uses a different runtime library which prevents loading numpy.

    Importing other packages from the application works

    Can you use other c-extension modules, for instance lxml?

  6. RichardEdye commented on Mar 6, 2019

    @RichardEdye
    Author

    Yes, importing lxml works with my app. And copying the DLL from .libs to core did not help. I've also changed the Python version to 3.6, but the problem remains. BTW, the pyd file in core is named "_multiarray_umath.cp36-win32.pyd".

    If it helps, I can upload my app for testing.

  7. mattip commented on Mar 6, 2019

    @mattip
    Member

    a link to the app repo or a pastebin with the code might be helpful, but I don't really use Windows these days so it might take a while to look at.

  8. RichardEdye commented on Mar 6, 2019

    @RichardEdye
    Author

    Ok, I've uploaded it here:

    https://opserver.de/down/NumPyTest.zip

    The Zip contains a folder "ZorroBeta". Unzip it anywhere on a Windows PC. It contains a lot of other stuff that won't matter here. Install Python 3.6. and Numpy on that PC. In the ZorroBeta folder, edit Zorro.ini and enter the path to Python 3.6 in double quotes next to "PythonPath". Then start the program Zorro.exe. It will open a panel. With the scrollbox marked "Script", select "NumPy". Then click "Test". Now go to the subfolder "Log". It contains two files PyError.txt and PyOut.txt. PyError.txt should be empty when the Numpy import worked. Otherwise it contains the error message.

    Alternatively the problem can be already checked with the simple main() example in the Python manual.

  9. modified the milestones: , on Apr 22, 2019
  10. elikosan commented on Apr 24, 2019

    @elikosan

    I have the same problem. I have an application with embedded python. When trying to load numpy, it fails with the same error. And it works fine from the command line python.

    I found that it also fails to load pytorch, with a similar error. The common issue i found is that the failed files have an extension made by concatenating the python version and the platform (e.g. cp37-win_amd64).

    Since the PATH are fine, i think the problem is that the embedded python does not know how to build that extension. I'm trying to dig this out, but to no avail so far. But it seems that the problem is in Python rather than numpy (or pytorch).

    In numpy, the failed command is:
    from . import _multiarray_umath
    and the corresponding file is:
    C:\Program Files\Python37\Lib\site-packages\numpy\core\ _multiarray_umath.cp37-win_amd64.pyd

    In pytorch, the failed command is:
    from torch._C import *
    and the corresponding file is:
    C:\Program Files\Python37\Lib\site-packages\torch\ _C.cp37-win_amd64.pyd

  11. mattip commented on Apr 25, 2019

    @mattip
    Member

    Is there a missing \ between core and _multiarray_umath? The pyd file should sit in the core directory.

  12. elikosan commented on Apr 25, 2019

    @elikosan

    Yes. The github web editor seems to eat the backslash when rendering a backslash followed by an underscore. I added a space.

  13. mattip commented on May 1, 2019

    @mattip
    Member

    Install Python 3.6. and Numpy on that PC ...

    Are you mixing python 3.6 and python 3.7?

  14. 19 remaining items

  15. added this to the 1.17.2 release milestone on Aug 23, 2019
  16. mattip commented on Dec 3, 2019

    @mattip
    Member

    Closing. This issue started about trying to use a release version of NumPy on a debug version of windows python, and transitioned to talking about reimporting after calling Py_Finalize();, which is not supported.

    Please open a new issue if there is something I missed.

  17. RichardEdye commented on Dec 3, 2019

    @RichardEdye
    Author

    Well, I fear it has absolutely nothing to do with debug or release versions. But this is a quite old thread and my software was delivered long ago. So for me the issue is closed.

    I just had to write in the documentation that NumPy does not clean exit, so for starting it twice the user had to close and restart the whole application, or use a different vector library. Fortunately, only Numpy had this problem. I never encountered it with other Python math libraries.

  18. alkalinin commented on Feb 29, 2020

    @alkalinin

    This is a problem of the 'pyd' naming convention, see python issue 36716.

    import importlib.machinery
    print(importlib.machinery.all_suffixes())

    CommandLine Python:

    > ['.py', '.pyw', '.pyc', '.cp37-win_amd64.pyd', '.pyd']

    C++ Embedded Python:

    > ['.py', '.pyw', '.pyc', '_d.cp37-win_amd64.pyd', '_d.pyd']
  19. spartha77 commented on Apr 8, 2020

    @spartha77

    I too had the same problem. I solved it.
    My problem statement: (Windows)

    -> I had installed Python3.5 at C:\Python35,
    -> I invoked Python and ran "import numpy"
    It gave rise to similar problem.
    << I then realized that the numpy installation was at C:\Users\partha3x\AppData\Roaming\Python\Python37\site-packages\numpy\core\ and NOT from the path I had mentioned at the time of installation.
    -> So, I modified (re-installed) python at the user profile home directory.
    -> The home profile directory is
    C:\Users\partha3x\AppData\Local\Programs\Python\Python37
    ->Uninstalled the numpy
    ->Re-installed numpy
    With this "import numpy" call succeeded in python

    With Visual Studio 2019, I had the same problem. I followed the following steps
    -> Uninstalled all Pythons that which were installed using VIsual Studio installer. (Not sure if this step helped me)
    -> In the project properties, in which I was invoking Python (embedded Python) from C++ project, I had set additional Libraries ( Project properties->Linker->General->Additional Library Directories I added C:\Users\partha3x\AppData\Roaming\Python\Python37\site-packages\numpy\core\lib AND -> In the project properties -> Linker->Input->Additional Dependencies , I added npymath.lib.

    These fixed the issues.

    In Linux, similar to the project properties, the files have to be compiled with additional dependencies with additional directories using make files.

    Hope this helps.

  20. MyFirstGitEver commented on Jan 31, 2024

    @MyFirstGitEver

    My problem was that I was building the embedded python in Debug mode. In this case python appends a _d to the suffixes when loading files. However, numpy installation did not include those files, so the import would fail.

    I'm also stuck with this problem. Could you help me? What do you mean by "I was building the embedded python in Debug mode". How can I change that? I tried to switch to release mode and the include lines just turn red for some reason. They wont let me compile

  21. MyFirstGitEver commented on Jan 31, 2024

    @MyFirstGitEver

    Have u checked if it's the release or debug problem? I've met the same situation as you did, using python 3.7.4 and numpy 1.17 (installed through pip) on windows. In C++ program, check sys.path of embedded python to see if it is looking for a "_d" suffix. And changing from debug mode to release mode solved my problem.

    I switched to release mode. Now Py_initialize throws "Could not find platform independent libraries <prefix"

  22. RichardEdye commented on Feb 1, 2024

    @RichardEdye
    Author
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    00 - BugEmbedded

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions