I’m attempting to import the latest version of NumPy (v1.24.2 as of today) in Python 3.10. I’ve set up a virtual environment via Python’s built-in venv module, however, it throws the following import error:
Traceback (most recent call last):
File "C:\\mypython\\myvenv\\lib\\site-packages\\numpy\\core\\\__init \_\_.py", line 23, in \<module\> from . import multiarray
File "C:\\mypython\\myvenv\\lib\\site-packages\\numpy\\core\\multiarray.py", line10, in \<module\> from . import overrides
File "C:\\mypython\\myvenv\\lib\\site-packages\\numpy\\core\\overrides-py", line 6, in \<module\> from numpy.core.\_multiarray_umath import (ImportError: dynamic module does not define module export function (PyInit__multiarray_umath)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C: \\mypython\\test \\hello.py", line 19, in module› import numpy as np
File "C: \\mypython myvenv\\1ib\\site-packages \\numpy\\\_init\_.py", line 141, in ‹module› from . import core
File "C: \\mypython \\myvenv \\lib\\site-packages \\numpy\\core\\\_init\_.py“ 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.10 from "C: \\mypython \\myvenv\\Scripts \\python.exe"
- The NumPy version is: "1.24.2"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: dynamic module does not define module export function (PyInitmultiarray_umath)I have also attempted to import outside the virtual environment. This seems like a unique issue as I have not seen it elsewhere.
The above response includes some documentation on troubleshooting NumPy import errors. As per this documentation, please note the following:
The Python SDK was installed by my company's IT department in this directory C:\Program Files\Python310. It might be worth noting this setup works perfectly fine on my home machine albeit with Python 3.11. However, this setup should still work with Python 3.10 as per NumPy’s documentation.
I installed NumPy via pip (Python's built-in package manager)
My office machine is a Windows 64-bit operating system
Only one version of Python is installed (Python 3.10)
I have not built this from source, so disclosing compiler versions is not applicable
I'm not too familiar with environment variables re PATH and PYTHONPATH so any advice is encouraged. That said, the directory of my virtual environment is in PATH alongside other critical directories (e.g. system installation of Python), however, I noticed my PYTHONPATH is showing as "None". Perhaps this may be where the issue lies?
As NumPy is critical for my work this is somewhat of an emergency, so hoping to get some much needed advice as to what is going on.
For those wondering, I managed to resolve this by getting my company's IT department to install Numpy with admin rights.
Due to not having admin privileges, when I installed Numpy it was redirected and saved to a site-packages directory that was different to the system site-packages directory. The file path of this was C:\Users<user>\AppData\Roaming\Python310\Lib\site-packages. I suspect this was not on my PATH.
Note the file path of my system site-packages directory is located in Program Files which is a special Windows folder that requires admin rights to modify (e.g. add/delete files). Consequently, when adding files to Program Files (in my case installing Numpy) without admin rights, these get redirected to the aforementioned APPDATA directory.
FYI - if you can't get direct/indirect access to admin privileges then I'd suggest installing Python via Anaconda, a Python distribution that will pre-install Numpy and other popular libraries at the point of system installation.