4
votes

I usually work with python 2.7 but this time i have to test a script in python3.

It is already installed on my computer, however when i start "python3", then go "import numpy", it show me "cannot import name 'multiarray'.

I even installed anaconda3 to test, but nothing happens

myName:~/anaconda3/bin$ python3
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/numpy/__init__.py", line 180, in     <module>
    from . import add_newdocs
  File "/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/usr/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/usr/lib/python2.7/dist-packages/numpy/core/__init__.py", line 14, in <module>
    from . import multiarray
ImportError: cannot import name 'multiarray'

I saw that it is looking in the lib/python2.7, but i cannot find what to do to get him search in the python3 library.

I already tried python -m pip install numpy, and tried to create a virtualenv in python3 but i still get the same error.

I cannot figure what to do. Can someone help me ?

I would like to add, i cannot start command with 'sudo' as i'm working on a client machine.

edit:

i tried @gehbiszumeis answer and got this:

myName:~ $ cd anaconda3/bin/
myName:~/anaconda3/bin $ source activate /home/myName/anaconda3
(base) myName:~/anaconda3/bin $ conda list numpy
# packages in environment at /home/myName/anaconda3:
#
# Name                    Version                   Build  Channel
numpy                     1.14.3           py36hcd700cb_1  
numpy-base                1.14.3           py36h9be14a7_1  
numpydoc                  0.8.0                    py36_0  
(base) myName:~/anaconda3/bin $ python3
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/numpy/__init__.py", line 180, in <module>
    from . import add_newdocs
  File "/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/usr/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/usr/lib/python2.7/dist-packages/numpy/core/__init__.py", line 14, in <module>
    from . import multiarray
ImportError: cannot import name 'multiarray'

I see there is numpy 36 installed when i type conda list numpy, but it seems not to work.. Did i miss something ?

edit2: After @Pal Szabo method, i tested command python3 -m pip install --upgrade pip and got this error :

(env) (base) myName:~/anaconda3/bin $ python3 -m pip install --upgrade pip
Traceback (most recent call last):
  File "/home/myName/anaconda3/lib/python3.6/runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/home/myName/anaconda3/lib/python3.6/runpy.py", line 142, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/home/myName/anaconda3/lib/python3.6/runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 4, in <module>
    import locale
  File "/home/myName/anaconda3/bin/env/lib/python3.6/locale.py", line 16, in <module>
    import re
  File "/home/myName/anaconda3/bin/env/lib/python3.6/re.py", line 142, in <module>
    class RegexFlag(enum.IntFlag):
AttributeError: module 'enum' has no attribute 'IntFlag'`

It is a crazy mix between python3, python2.7 then again python3. I'm lost \o/

edit3:

I finally found my error. It was a probleme with my PYTHONPATH, which was pointing somewhere where a .pth file was defined, with some hard link to python 2.7 libraries. with a simple "unset PYTHONPATH" it works fine. Thanks you all

4

4 Answers

2
votes

I had the same problem, took me several hours to figure it out.

In my case, the PYTHONPATH was set to /usr/lib/python2.6/dist-packages/ changing it to /Users/xxx/miniconda3/lib/python3.7/site-packages/ resolved the issue. Good luck.

1
votes

Based what I can see from your code listing, it seems that the anaconda environment is not activated (this is usually indicated by the environments name in surrounding brackets () before the prompt). Because of that python3 tries to look for numpy and only finds the one you used for python 2.7 previously, which causes the error (see the python2.7 error messages)

Try

source activate <your-anaconda-environment>

Then your prompt should look like this

(<your-anaconda-environment>) myName:~/anaconda3/bin$ 

Numpy should be contained in the standard anaconda environment. Try then to check if numpy is installed within your environment with

conda list numpy

If there is no numpy listed, try to install it with pip as you wrote before.

0
votes

If want to use python3, use pip3 instead of pip:

 pip3 install numpy

You can try these too in terminal:

rm -Rf env
virtualenv -p python3 env
source env/bin/activate
python3 -m pip install --upgrade pip
pip3 install numpy

Also make sure that the first line of your script is

#!/usr/bin/python3

Don't write anything, even comments, before this.

0
votes

I solved this problem changing the Python's version on VS Code. I was working with Python 3.7.4, now with the 3.7.8 one.

When I installed Numpy, it was stored in the folder of Python3.6. So I think that u could try to change the path or just change the Python's version