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?
You probably have
numpy
installed only for your python3.5.Instead of installing
numpy
through the Ubuntu repositories, you could try installing and usingpip
.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:Note: You may receive a
Permission denied
error. In this case just prefix your command withsudo
e.g.: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
globallyInstall
numpy
specifically for python3.6:Note: Again, if you receive a permission error, prefix your command with
sudo
:The output:
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:
Again, check if the installation is successful:
Should print the version without any error messages.
Now create a virtual environment for python3.6 (FYI - there are multiple ways to achieve that):
e.g.
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:
The name of the virtual environment should appear on the left side of your terminal e.g.:
It indicates that the virtual environment is currently active.
Ok, now you need install
numpy
:Output:
Now start your Python shell and try to import it:
There should be no errors.
You can now continue with your work.
When you're done you can deactivate the virtual environment. Just type:
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.
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:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/<python version> 1
This will make pip3 work for your desired version of python3
sudo pip3 uninstall numpy
sudo pip3 install numpy
This worked!
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: