I have tried importing NumPy in Python, but it did not succeed:
>>> import numpy as np
x=np.array([[7,8,5],[3,5,7]],np.int32)
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
import numpy as np
File "C:\Python27\lib\numpy\__init__.py", line 127, in <module>
raise ImportError(msg)
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.
How can I fix this?
The message is fairly self-explanatory; your working directory should not be the NumPy source directory when you invoke Python; NumPy should be installed and your working directory should be anything but the directory where it lives.
On Debian/Ubuntu:
aptitude install python-numpy
On Windows, download the installer:
http://sourceforge.net/projects/numpy/files/NumPy/
On other systems, download the tar.gz and run the following:
$ tar xfz numpy-n.m.tar.gz
$ cd numpy-n.m
$ python setup.py install
Related
I am taking a course that uses a racecar simulator and I need to run some pre-built code that uses a custom library and also numpy. When I try to run the code on Ubuntu I get the following error:
Traceback (most recent call last):
File "/mnt/c/Users/seb/documents/Racecar/racecar-seb-h-s/labs/../library/numpy/core/__init__.py", line 22, in <module>
from . import multiarray
File "/mnt/c/Users/seb/documents/Racecar/racecar-seb-h-s/labs/../library/numpy/core/multiarray.py", line 12, in <module>
from . import overrides
File "/mnt/c/Users/seb/documents/Racecar/racecar-seb-h-s/labs/../library/numpy/core/overrides.py", line 7, 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:
Traceback (most recent call last):
File "demo.py", line 16, in <module>
import racecar_core
File "/mnt/c/Users/seb/documents/Racecar/racecar-seb-h-s/labs/../library/racecar_core.py", line 13, in <module>
import camera
File "/mnt/c/Users/seb/documents/Racecar/racecar-seb-h-s/labs/../library/camera.py", line 11, in <module>
import numpy as np
File "/mnt/c/Users/seb/documents/Racecar/racecar-seb-h-s/labs/../library/numpy/__init__.py", line 150, in <module>
from . import core
File "/mnt/c/Users/seb/documents/Racecar/racecar-seb-h-s/labs/../library/numpy/core/__init__.py", line 48, 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.8 from "/usr/bin/python3"
* The NumPy version is: "1.21.2"
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'
I have tried all the other potential duplicates that I could find and used the link but none worked. I have also tried uninstalling and reinstalling and updating numpy to no avail.
The output of running the command echo $PATH was the following:
/home/seb/.local/bin
:/usr/local/sbin
:/usr/local/bin
:/usr/sbin
:/usr/bin
:/sbin
:/bin
:/usr/games
:/usr/local/games
:/mnt/c/Program Files/WindowsApps/CanonicalGroupLimited.UbuntuonWindows_2004.2021.825.0_x64__79rhkp1fndgsc:/mnt/c/Program Files/Java/jdk-13.0.1/bin.
:/mnt/c/Program Files (x86)/Common Files/Oracle/Java/javapath
:/mnt/c/Program Files (x86)/Common Files/Intel/Shared Libraries/redist/intel64/compiler:/mnt/c/WINDOWS/system32
:/mnt/c/WINDOWS
:/mnt/c/WINDOWS/System32/Wbem
:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/
:/mnt/c/WINDOWS/System32/OpenSSH/
:/mnt/c/Users/seb/anaconda3
:/mnt/c/Users/seb/anaconda3/Library/mingw-w64/bin
:/mnt/c/Users/seb/anaconda3/Library/usr/bin
:/mnt/c/Users/seb/anaconda3/Library/bin
:/mnt/c/Users/seb/anaconda3/Scripts
:/mnt/c/Users/seb/AppData/Local/Microsoft/WindowsApps
:/snap/bin
The output of running the command python3 -m site was the following:
sys.path = [
'/mnt/c/Users/seb/documents/Racecar/racecar-seb-h-s/labs',
'/usr/lib/python38.zip',
'/usr/lib/python3.8',
'/usr/lib/python3.8/lib-dynload',
'/home/seb/.local/lib/python3.8/site-packages',
'/usr/local/lib/python3.8/dist-packages',
'/usr/lib/python3/dist-packages',
]
USER_BASE: '/home/seb/.local' (exists)
USER_SITE: '/home/seb/.local/lib/python3.8/site-packages' (exists)
ENABLE_USER_SITE: True
This has happened to me as well because I am taking the same course. I solved it by uninstalling all the libraries and installing them again. Here is the link to the setup instructions: https://mitll-racecar-mn.readthedocs.io/en/latest/gettingStarted/computerSetup/python.html. These are the commands you have to run to install the libraries:
sudo apt update
sudo apt install python3.8
sudo apt install python3-pip
pip3 install --upgrade pip
pip3 install numpy
pip3 install matplotlib
pip3 install mypy
pip3 install nptyping
pip3 install opencv-contrib-python
pip3 install jupyter
pip3 install ipywidgets
jupyter nbextension enable --py widgetsnbextension
If i run pip install numpy then I get
Requirement already satisfied: numpy in /Users/aaditya/.pyenv/versions/3.8.5/lib/python3.8/site-packages (1.19.5)
But if I use import numpy as np in a python file then I get
Traceback (most recent call last):
File "/Users/aaditya/Desktop/Blender_software/Blender_algo_exp/quantity_sol.py", line 1, in <module>
import numpy as np
ModuleNotFoundError: No module named 'numpy'
Although if I run import numpy as np in my terminal directly then it works. Is there any way to fix this?
I think you just need restart Visual Code or Pycharm
Or you should check python IDE for Visual Studio
Another solution is run command
python3 -m pip install numpy
I want to zip some sources in order to use it easily from multiple locations. I have to install some external modules, let us say pyyaml and numpy.
First, I install these packages in a specific location:
python -m pip install --target=lib pyyaml numpy
And then I create the zipped library:
python -m zipapp library --output=my_library.pyd --compress
In another interpreter, after adding "my_library.pyd" to the sys.path, I can load the yaml module, but not the numpy one:
>>> import sys
>>> from pathlib import Path
>>> sys.path.append(str(Path().absolute() / 'my_library.pyd'))
>>> import yaml
>>> import numpy
Traceback (most recent call last):
File "D:\Documents\rdpy\packaging_tests\my_library.pyd\numpy\core\__init__.py", line 40, in <module>
File "D:\Documents\rdpy\packaging_tests\my_library.pyd\numpy\core\multiarray.py", line 12, in <module>
File "D:\Documents\rdpy\packaging_tests\my_library.pyd\numpy\core\overrides.py", line 6, in <module>
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\Documents\rdpy\packaging_tests\my_library.pyd\numpy\__init__.py", line 142, in <module>
File "D:\Documents\rdpy\packaging_tests\my_library.pyd\numpy\core\__init__.py", line 71, in <module>
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
Here is how to proceed:
- If you're working with a numpy git repository, try `git clean -xdf`
(removes all files not under version control) and rebuild numpy.
- If you are simply trying to use the numpy version that you have installed:
your installation is broken - please reinstall numpy.
- If you have already reinstalled and that did not fix the problem, then:
1. Check that you are using the Python you expect (you're using D:\Apps\python-3.7\python.exe),
and that you have no directories in your PATH or PYTHONPATH that can
interfere with the Python and numpy versions you're trying to use.
2. If (1) looks fine, you can open a new issue at
https://github.com/numpy/numpy/issues. Please include details on:
- how you installed Python
- how you installed numpy
- your operating system
- whether or not you have multiple versions of Python installed
- if you built from source, your compiler versions and ideally a build log
Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.
Original error was: No module named 'numpy.core._multiarray_umath'
In the zip archive, I do have a file named numpy\core\_multiarray_umath.cp37-win_amd64.pyd, but it obviously is a C compiled file.
Is there anything I can do to be able and import numpy when creating the zip archive?
I believe this is a problem with zipapp on import some .c compiled files. To solve this I used the install creator. It was what I want, create a .exe from python code.
I am using qiskit (I am an absolute beginner with some experience with Java and know basic python) but when I tried to run an example code which uses numpy it gave me this error. I tried reinstalling it but it gave me the same error.
I have tried installing and reinstalling it but it didnt work
import numpy as np
from cqc.pythonLib import CQCConnection, qubit
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, execute
from qiskit.tools.visualization import circuit_drawer
from qiskit.quantum_info import state_fidelity
from qiskit import BasicAer
Traceback (most recent call last):
File "/home/nikhil/.local/lib/python3.6/site-packages/numpy/core/__init__.py", line 40, in <module>
from . import multiarray
File "/home/nikhil/.local/lib/python3.6/site-packages/numpy/core/multiarray.py", line 12, in <module>
from . import overrides
File "/home/nikhil/.local/lib/python3.6/site-packages/numpy/core/overrides.py", line 6, 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:
Traceback (most recent call last):
File "/home/nikhil/.local/lib/python3.6/site-packages/fghjkl.py", line 1, in <module>
import numpy as np
File "/home/nikhil/.local/lib/python3.6/site-packages/numpy/__init__.py", line 142, in <module>
from . import core
File "/home/nikhil/.local/lib/python3.6/site-packages/numpy/core/__init__.py", line 71, in <module>
raise ImportError(msg)
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
Here is how to proceed:
- If you're working with a numpy git repository, try `git clean -xdf`
(removes all files not under version control) and rebuild numpy.
- If you are simply trying to use the numpy version that you have installed:
your installation is broken - please reinstall numpy.
- If you have already reinstalled and that did not fix the problem, then:
1. Check that you are using the Python you expect (you're using /usr/bin/python3.7),
and that you have no directories in your PATH or PYTHONPATH that can
interfere with the Python and numpy versions you're trying to use.
2. If (1) looks fine, you can open a new issue at
https://github.com/numpy/numpy/issues. Please include details on:
- how you installed Python
- how you installed numpy
- your operating system
- whether or not you have multiple versions of Python installed
- if you built from source, your compiler versions and ideally a build log
Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.
Original error was: No module named 'numpy.core._multiarray_umath'
Try to upgrade the numpy:
pip install --upgrade numpy
or uninstall it and reinstall it again:
pip uninstall numpy
pip install numpy
I'm tired to import cv and numpy and get errors. I started importing cv and I got this error:
ImportError: numpy.core.multiarray failed to import
Traceback (most recent call last):
File "", line 1, in <module>
File "/usr/lib/pymodules/python2.7/cv.py", line 1, in <module>
from cv2.cv import *
ImportError: numpy.core.multiarray failed to import
So I installed numpy on Ubuntu using:
apt-get install python-numpy
So when I import numpy I get:
Traceback (most recent call last):
File "", line 1, in <module>
File "numpy/init.py", line 127, in <module>
raise ImportError(msg)
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 intepreter from there.
I really need help. I'm using Python 2.7.3 on Ubuntu.
There is nothing wrong with installing common dependencies using your operating system's package manager, remember using pip means your synaptic updates won't update your Python libraries and pip won't leverage the dependencies already taken care of by aptitude. For python packages with C extensions like numpy and opencv its probably better to use apt-get.
In Ubuntu you can install both dependencies with
sudo apt-get install python-numpy python-opencv
The actual Python error you are getting indicates what is wrong, namely that you are executing from within the numpy source directory, or have a file named numpy.py in your current directory which is confusing things at import time. Try change into an empty directory, start Python import your libraries:
import numpy
import cv
Hope that helps.
It's better that use some package management tool such like pip to install numpy.
For example,
pip install numpy