Cannot import numpy library in Python 3.8.0 - python

I'm attempting to load a numpy library into python 3.8.0 on Ubuntu.
from numpy import loadtxt
gives a very verbose error, without a solution:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/numpy/core/__init__.py", line 16, in <module>
from . import multiarray
ImportError: cannot import name 'multiarray' from partially initialized module 'numpy.core' (most likely due to a circular import) (/usr/lib/python3/dist-packages/numpy/core/__init__.py)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/numpy/__init__.py", line 142, in <module>
from . import add_newdocs
File "/usr/lib/python3/dist-packages/numpy/add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "/usr/lib/python3/dist-packages/numpy/lib/__init__.py", line 8, in <module>
from .type_check import *
File "/usr/lib/python3/dist-packages/numpy/lib/type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "/usr/lib/python3/dist-packages/numpy/core/__init__.py", line 26, in <module>
raise ImportError(msg)
ImportError:
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control). Otherwise reinstall numpy.
Original error was: cannot import name 'multiarray' from partially initialized module 'numpy.core' (most likely due to a circular import) (/usr/lib/python3/dist-packages/numpy/core/__init__.py)
Based on advice that I saw on Python circular importing? I changed the import line to import all of numpy:
import numpy
but this gives the same error.
As the error suggests, I tried re-installing numpy with pip:
sudo pip install --upgrade --force-reinstall numpy
the same error happens with
sudo pip3 install --upgrade --force-reinstall numpy
which worked, and installed numpy-1.19.5. However, this didn't fix the import problem.
I tried implementing a solution from https://github.com/numpy/numpy/issues/9047 which involves setting
export PYTHONPATH=/usr/lib/python3/dist-packages/$PYTHONPATH in ~/.bashrc, but this failed as well.
How can I import numpy?

The problem was that pip and python were linked to different versions.
The solution was to install via sudo /usr/bin/python3.8 -m pip uninstall numpy and then sudo /usr/bin/python3.8 install numpy

After looking into circular imports for a bit, it looks like you might just be better off doing import numpy and altering your code accordingly rather than using the from syntax

Related

Python ImportError "cannot import name 'multiarray'

I'm trying to simply import numpy into a python script (called "MatPlotLib.py using PyCharm) and I'm receiving this odd error. It worked fine, but then I started messing around with Jupyter. I tried uninstalling and reinstalling Python, then a system restore, but nothing seems to be working. I think reinstalling numpy will do the trick, though I'm not sure how to do that.
I'm not sure what the error means by "a numpy git repo, so any help would be greatly appreciated. If you need any additional information please let me know. Find the full error below! :)
Thank you so much in advanced.
C:\ProgramData\Anaconda3\python.exe
C:/Users/Alex/PycharmProjects/2017_Research/Automation/MatPlotLib.py
Traceback (most recent call last): File
"C:\Users\Alex\AppData\Roaming\Python\Python36\site-packages\numpy\core__init__.py",
line 16, in
from . import multiarray ImportError: cannot import name 'multiarray'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"C:/Users/Alex/PycharmProjects/2017_Research/Automation/MatPlotLib.py",
line 1, in
import numpy File "C:\Users\Alex\AppData\Roaming\Python\Python36\site-packages\numpy__init__.py",
line 142, in
from . import add_newdocs File "C:\Users\Alex\AppData\Roaming\Python\Python36\site-packages\numpy\add_newdocs.py",
line 13, in
from numpy.lib import add_newdoc File "C:\Users\Alex\AppData\Roaming\Python\Python36\site-packages\numpy\lib__init__.py",
line 8, in
from .type_check import * File "C:\Users\Alex\AppData\Roaming\Python\Python36\site-packages\numpy\lib\type_check.py",
line 11, in
import numpy.core.numeric as _nx File "C:\Users\Alex\AppData\Roaming\Python\Python36\site-packages\numpy\core__init__.py",
line 26, in
raise ImportError(msg) ImportError: Importing the multiarray numpy extension module failed. Most likely you are trying to import a
failed build of numpy. If you're working with a numpy git repo, try
git clean -xdf (removes all files not under version control).
Otherwise reinstall numpy.
Original error was: cannot import name 'multiarray'
Process finished with exit code 1

numpy in Python 3.6: Importing the multiarray numpy extension module failed

Can anyone help me with this error? I don't have any older versions of numpy, and i installed it using pip, i get 1.13.0 version of numpy and I'm working on Windows 10 OS. Any suggestions?
Traceback (most recent call last):
File "C:/Users/Asus/PycharmProjects/g/num.py", line 1, in <module>
import numpy
File "C:\Users\Asus\AppData\Local\Programs\Python\Python36-32\lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import add_newdocs
File "C:\Users\Asus\AppData\Local\Programs\Python\Python36-32\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "C:\Users\Asus\AppData\Local\Programs\Python\Python36-32\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
from .type_check import *
File "C:\Users\Asus\AppData\Local\Programs\Python\Python36-32\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "C:\Users\Asus\AppData\Local\Programs\Python\Python36-32\lib\site-packages\numpy\core\__init__.py", line 26, in <module>
raise ImportError(msg)
ImportError:
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control). Otherwise reinstall numpy.
Original error was: DLL load failed: The specified procedure could not be found.
You are, apparently, a victim of issue 9272. numpy is built against Python 3.6.1 but you're using Python 3.6.0.
Based on the discussion there, you should just upgrade to Python 3.6.1 to get it to work. It does seem like they will build on 3.6.0 too, though, follow that issue to keep track of that.

python virtualenv scipy import error undefined name

I just started using virtualenv for my existing python project and ran into some trouble...
When I try to import the following
from scipy.sparse.linalg import spsolve
it causes an import error if a virtualenv is activated
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../lib/python2.7/site-packages/scipy/sparse/linalg/__init__.py", line 110, in <module>
from .dsolve import *
File ".../lib/python2.7/site-packages/scipy/sparse/linalg/dsolve/__init__.py", line 60, in <module>
from .linsolve import *
File ".../lib/python2.7/site-packages/scipy/sparse/linalg/dsolve/linsolve.py", line 10, in <module>
from . import _superlu
ImportError: .../lib/python2.7/site-packages/scipy/sparse/linalg/dsolve/_superlu.so: undefined symbol: dtrsm_
When I use the global site-packages I don't get the error.
Can someone help me?
It appears there's some sort of trouble between numpy-1.10.2 and scipy (see here). Try the following (it fixed it for me):
(ve) $ pip install numpy==1.10.1
(ve) $ pip install --upgrade --force-reinstall scipy
(ve) $ python
>>> from scipy.sparse.linalg import spsolve

Error importing numpy after upgrading with pip

This question is related to a question here: Unable to import numpy
I ran: sudo pip install --upgrade numpy
then when I ran python and tried to import numpy I got this strange error!!! I'm going crazy!
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/__init__.py", line 153, in <module>
from . import add_newdocs
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/__init__.py", line 18, in <module>
from .polynomial import *
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/polynomial.py", line 19, in <module>
from numpy.linalg import eigvals, lstsq, inv
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/linalg/__init__.py", line 50, in <module>
from .linalg import *
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/linalg/linalg.py", line 29, in <module>
from numpy.linalg import lapack_lite, _umath_linalg
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/linalg/lapack_lite.so, 2): Symbol not found: __gfortran_compare_string
Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/linalg/lapack_lite.so
Expected in: flat namespace
in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/linalg/lapack_lite.so
>>>
EDIT:
I also ran this:
$ pip show numpy
---
Name: numpy
Version: 1.8.0
Location: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
Requires:
The fix for me to was to manually install gcc via macports:
sudo port install gcc48
... which installs gfortran, but without the alias that numpy seems to be expecting (gfortran). Then we make a symlink:
sudo ln -s /opt/local/bin/gfortran-mp-4.8 /opt/local/bin/gfortran
After that, I uninstalled and reinstalled numpy via pip and it works like a charm.
This fixed the issue for me on mavericks last night. From what I've read, gfortran used to be part of the OS X command line tools, but isn't as of Lion or so. Installing gcc separately "fixes" that.

ATpy ImportError: No module named astropy.io

I tried to get atpy on python 2.7. I downloaded ATpy0.9.6 from
https://pypi.python.org/pypi/ATpy/0.9.6
then tar it, and run:
cd ATpy0.9.6
sudo python setup.py install
things seemed to work well.
But when I tried to import atpy on python, I got always this error message:
ImportError: No module named astropy.io
More precisely,
>>> import atpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/atpy/__init__.py", line 1, in <module>
from .basetable import Table, TableSet, VectorException
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/atpy/basetable.py", line 15, in <module>
from . import registry
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/atpy/registry.py", line 164, in <module>
from . import fitstable
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/atpy/fitstable.py", line 8, in <module>
from astropy.io import fits
ImportError: No module named astropy.io
>>>
Could anyone help me to figure out the problem?
Perhaps the 'atpy' package depends on another package called 'astropy.io' that you are missing? Yes here it is https://astropy.readthedocs.org/en/v0.1/index.html
It happens that downloading the tar balls from pypi.python.org is the wrong way to install Python packages. The right way (though the Python docs explain poorly), is to run a command like
pip install atpy
This command will install your package and all its dependencies.
If the command pip is missing, you have to install it, which is ironically really complicated.
In my case the solution was to install astropy
pip install --no-deps astropy

Categories

Resources