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.
Related
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
While trying to import sklearn, i come across the following error message :
AttributeError: module 'numpy' has no attribute '__config__'
I did install and updated both numpy and sklearn with conda but still get the same error. Any ideas?
import sklearn
Get error message:
Traceback (most recent call last):
File "<ipython-input-35-b7c74cbf5af0>", line 1, in <module>
import sklearn
File "C:\Users\sound\Documents\Conda\lib\site-packages\sklearn\__init__.py", line 82, in <module>
from .base import clone
File "C:\Users\sound\Documents\Conda\lib\site-packages\sklearn\base.py", line 20, in <module>
from .utils import _IS_32BIT
File "C:\Users\sound\Documents\Conda\lib\site-packages\sklearn\utils\__init__.py", line 20, in <module>
from scipy.sparse import issparse
File "C:\Users\sound\Documents\Conda\lib\site-packages\scipy\sparse\__init__.py", line 229, in <module>
from .base import *
File "C:\Users\sound\Documents\Conda\lib\site-packages\scipy\sparse\base.py", line 7, in <module>
from scipy._lib._numpy_compat import broadcast_to
File "C:\Users\sound\Documents\Conda\lib\site-packages\scipy\_lib\_numpy_compat.py", line 16, in <module>
_assert_warns = np.testing.assert_warns
File "C:\Users\sound\Documents\Conda\lib\site-packages\numpy\__init__.py", line 213, in __getattr__
import numpy.testing as testing
File "C:\Users\sound\Documents\Conda\lib\site-packages\numpy\testing\__init__.py", line 12, in <module>
from ._private.utils import *
File "C:\Users\sound\Documents\Conda\lib\site-packages\numpy\testing\_private\utils.py", line 57, in <module>
HAS_LAPACK64 = hasattr(numpy.__config__, 'lapack_ilp64_opt_info')
File "C:\Users\sound\Documents\Conda\lib\site-packages\numpy\__init__.py", line 220, in __getattr__
"{!r}".format(__name__, attr))
AttributeError: module 'numpy' has no attribute '__config__'
---
I had the exact same problem using the binary numpy 1.18.1 package from piwheels on my RasperryPi 4. Do you also happen to try to use this on a Raspberry Pi?
If yes, then:
It works after uninstalling numpy and then retrieving numpy 1.18.1 from pypi instead of from piwheels (had to compile from source though - it takes roughly 15 minutes).
There seems to be something wrong with the piwheels package - I don't know what it is.
The following two commands should solve your problem:
pip3 uninstall numpy
pip3 install numpy==1.18.1 --no-binary :all:
The first command uninstalls your numpy package. The second one retrieves numpy version 1.18.1 where the switch --no-binary :all: tells pip to not get any binary packages, i.e. from piwheels, but instead compile from source.
I'm getting an ImportError whenever I try to import pyplot from matplotlib. Matplotlib is installed via pip in a virtual environment. This is the error I'm getting:
>>> import matplotlib.pyplot
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/mikefrank/.virtualenvs/pv-hrt/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 36, in <module>
from matplotlib.figure import Figure, figaspect
File "/home/mikefrank/.virtualenvs/pv-hrt/local/lib/python2.7/site-packages/matplotlib/figure.py", line 40, in <module>
from matplotlib.axes import Axes, SubplotBase, subplot_class_factory
File "/home/mikefrank/.virtualenvs/pv-hrt/local/lib/python2.7/site-packages/matplotlib/axes/__init__.py", line 4, in <module>
from ._subplots import *
File "/home/mikefrank/.virtualenvs/pv-hrt/local/lib/python2.7/site-packages/matplotlib/axes/_subplots.py", line 10, in <module>
from matplotlib.axes._axes import Axes
File "/home/mikefrank/.virtualenvs/pv-hrt/local/lib/python2.7/site-packages/matplotlib/axes/_axes.py", line 38, in <module>
import matplotlib.tri as mtri
File "/home/mikefrank/.virtualenvs/pv-hrt/local/lib/python2.7/site-packages/matplotlib/tri/__init__.py", line 9, in <module>
from .triangulation import *
File "/home/mikefrank/.virtualenvs/pv-hrt/local/lib/python2.7/site-packages/matplotlib/tri/triangulation.py", line 7, in <module>
import matplotlib._qhull as _qhull
ImportError: libqhull.so.5: cannot open shared object file: No such file or directory
This can occur after a system upgrade. In my specific case, I upgraded Ubuntu 14.04 to 16.04. Matplotlib links to specific versions of \*.so, and these go out of date. I presume this is because the different versions are not (always) backward compatible.
(For me, I went from libqhull.so.6 to libqhull.so.7 during the OS upgrade, and matplotlib was linked to libqhull.so.6.)
Firstly, you can probably make sure libqhull exists on your system by using something like:
sudo apt install libqhull-dev
or the equivalent for your system.
(mine was already installed, so nothing changed.)
Since nowadays, pip often installs a wheel, which is already compiled, we'll need to force pip to do the compilation, so it links with the new so file.
I used:
py35 -m pip install --upgrade matplotlib --no-binary matplotlib --force
The options:
--no-binary matplotlib tells pip to not use a wheel for matplotlib (but it may still use wheels for dependencies such as numpy).
--upgrade should be obvious
--force, because we may already be at the latest matplotlib version, and then --upgrade will not do anything.
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
I'm running Ubuntu 13.10 with Python 2.7.5+, and whenever I try to import the matplotlib package, I get an error. Specifically, my test script has only the following code:
#!/usr/bin/python
import matplotlib.pyplot as plt
When I run the test script, here is the error:
Traceback (most recent call last):
File "./test.py", line 2, in <module>
import matplotlib.pyplot as plt
File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 27, in <module>
import matplotlib.colorbar
File "/usr/local/lib/python2.7/dist-packages/matplotlib/colorbar.py", line 34, in <module>
import matplotlib.collections as collections
File "/usr/local/lib/python2.7/dist-packages/matplotlib/collections.py", line 27, in <module>
import matplotlib.backend_bases as backend_bases
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 56, in <module>
import matplotlib.textpath as textpath
File "/usr/local/lib/python2.7/dist-packages/matplotlib/textpath.py", line 22, in <module>
from matplotlib.mathtext import MathTextParser
File "/usr/local/lib/python2.7/dist-packages/matplotlib/mathtext.py", line 26, in <module>
from six import unichr
ImportError: cannot import name unichr
Any idea what I'm doing wrong?
You apparently have an outdated version of six.
The unichr wrapper was added in version 1.4.0, as seen in CHANGES. I'm not sure exactly when 1.4.0 was released, but the fix for issue #25 was committed on 2013-05-18, so… some time after that.
So, if you print(six.__version__), and it's anything less than '1.4.0', that's your problem.
Depending on whether you're installing packages with pip or with your system's package manager, the solution is going to be something like one of these:
$ pip install --upgrade six
$ apt-get install six
$ brew install --upgrade six
… etc.
But meanwhile, matplotlib shouldn't be requiring 1.3 but using features only available in 1.4. Your GitHub issue matplotlib#3538 should get this fixed.