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.