Wednesday, May 2, 2012

Installing NumPy 1.6 for Python 3 in Ubuntu 12.04

I have a fresh install of Ubuntu 12.04. I want to use NumPy with Python 3. Note that Ubuntu 12.04 ships with Python 2.7 under the 'python' namespace and also ships with Python 3 under the 'python3' namespace.

Go to terminal, check python version

$ python --version
Python 2.7.3
$ python3 --version
Python 3.2.3
We want to use Python 3, so for the rest of this tutorial, make sure you're using python3 and not just python.

Go into the Python 3 interpreter:

$ python3
Python 3.2.3 (default, Apr 12 2012, 21:55:50) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

See if you have NumPy already installed:

>>> from numpy import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named numpy
Nope, it's not there, so we need to install. Exit the Python interpreter.
>>> exit()

Install NumPy using apt-get

Instead of browsing web forums and finding the right package to download and compile, just let Ubuntu's built-in package manager, apt-get, do the work. This is just like installing NumPy for the standard python installation, but because we need NumPy for the Python3 installation, we'll affix the '3' where needed:
$ sudo apt-get install python3-numpy python3-scipy
Let it have all the dependencies it wants (just type 'yes' when prompted).

See if it worked

To see if it worked, go back into the Python interpreter, import NumPy, create a NumPy array, and make sure it's a NumPy array:
>>> from numpy import *
>>> type(array([1,2,4,5]))
<type 'numpy.ndarray'>

Yup it worked.

Conclusion

In Ubuntu 12.04, just use apt-get to install NumPy for python 3 using
$ sudo apt-get install python3-numpy python3-scipy
Everything just works.

5 comments:

  1. Fantastic stuff, man. You presented it in a clear and easily to follow manner while maintaining professionalism.

    Cheers

    ReplyDelete
  2. Coming from Windows 7 and knowing almost nothing about programming, you saved me.
    How can you know that it is python3-numpy and python3-scipy and not any other stuff like, python3 scipy or just scipy or scipy3 or whatever?

    Please answer me and keep up the good work.
    Cheers.

    ReplyDelete
  3. Bit late to be of help, but for the record - there's a tool called apt-file that supports such searches...

    sudo apt-get install apt-file
    apt-file --package-only find python3 | grep numpy

    ReplyDelete
  4. you give the clearest instruction!!! Thanks

    ReplyDelete

  5. Thanks for sharing amazing information about python .Gain the knowledge and hands-on experience in python online training

    ReplyDelete