2

I installed Python3.6 as described here:

sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get update
sudo apt-get install python3.6

Then I installed numpy as follows:

sudo apt-get install python3-numpy

I guess I can import numpy from Python3.5 but not from Python3.6

Python 3.6.5 (default, Mar 29 2018, 03:28:50) 
[GCC 5.4.0 20160609] 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/python3/dist-packages/numpy/__init__.py", line 180, 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 14, in <module>
    from . import multiarray
ImportError: cannot import name 'multiarray'

How to fix this issue?

Billal Begueradj
  • 6,011
  • 11
  • 39
  • 56

3 Answers3

2

You probably have numpy installed only for your python3.5.

Instead of installing numpy through the Ubuntu repositories, you could try installing and using pip.
This is python's package manager. You can use it to install various python libraries like numpy.
Then use it to install numpy for your python3.6.

Installing pip:

Install pip with the following command:

apt install python-pip

Note: You may receive a Permission denied error. In this case just prefix your command with sudo e.g.:

sudo apt install python-pip

Then type in your terminal pip3.6 to check if you have it installed correctly. It should list you all its available parameters.

There are generally two options from here.

Option 1 - install numpy globally

Install numpy specifically for python3.6:

pip3.6 install numpy

Note: Again, if you receive a permission error, prefix your command with sudo:

sudo pip3.6 install numpy

The output:

Collecting numpy
  Downloading https://files.pythonhosted.org/packages/71/90/ca61e203e0080a8cef7ac21eca199829fa8d997f7c4da3e985b49d0a107d/numpy-1.14.3-cp36-cp36m-manylinux1_x86_64.whl (12.2MB)
    100% |################################| 12.2MB 1.9MB/s 
Installing collected packages: numpy
Successfully installed numpy-1.14.3

Note: The drawback of this method is that you have numpy installed globally, which may result in undesirable effects at some point in the future, like troubles with different versions.

Option 2 - use a virtual environment:

This method allows you to create an isolated Python environment, a sandbox if you will, where you can install python packages, without worrying so much about dependencies, versions and permissions.

First, you need to install the module, required to create virtual environments:

sudo pip3.6 install virtualenv

Again, check if the installation is successful:

virtualenv --version

Should print the version without any error messages.

Now create a virtual environment for python3.6 (FYI - there are multiple ways to achieve that):

virtualenv /directory/to/place/the/virtual/environment

e.g.

virtualenv ~/Documents/numpy

Navigate to that directory. There should be several directories inside it. We're looking for the bin directory.
Now you need to activate that virtual environment:

source bin/activate

The name of the virtual environment should appear on the left side of your terminal e.g.:

(numpy) user@hostname:numpy$

It indicates that the virtual environment is currently active.

Ok, now you need install numpy:

pip3.6 install numpy

Output:

Collecting numpy
  Using cached https://files.pythonhosted.org/packages/71/90/ca61e203e0080a8cef7ac21eca199829fa8d997f7c4da3e985b49d0a107d/numpy-1.14.3-cp36-cp36m-manylinux1_x86_64.whl
Installing collected packages: numpy
Successfully installed numpy-1.14.3

Now start your Python shell and try to import it:

>>> import numpy
>>> 

There should be no errors.
You can now continue with your work.

When you're done you can deactivate the virtual environment. Just type:

deactivate

The indicator on the left side should be gone.

If something goes wrong with your virtual environment, just delete the directory that contains it and start over.

Note: The drawback of using virtual environments is that you always need to activate and deactivate them, but it drastically reduces the change of messing up your globally installed libraries and settings.

haralambov
  • 1,444
  • 1
  • 11
  • 12
1

I had exactly the same problem. I have python3.6 and python3.7 installed on my Ubuntu system. I had numpy installed using pip3. I got the same error when I tried importing numpy in python3.7.

I observed that following is line number 1 in /user/bin/pip3 is

#!/usr/bin/python3

and my python3 was pointing to /usr/bin/python3.6

Following worked for me:

  1. Update python3

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/<python version> 1

This will make pip3 work for your desired version of python3

  1. Uninstall the numpy that was installed for older python3

sudo pip3 uninstall numpy

  1. Install the numpy for the desired version of python3

sudo pip3 install numpy

This worked!

0

I had the same problem, I fixed this error by updating the numpy package as follows:

sudo pip install -U numpy


[NOTE]:

If after that you encountered with this error: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.

Do the following command:

sudo apt-get install python3-tk