Ubuntu – How to install numpy and scipy for python

pythonsoftware installation

I am tired to try to install numpy and scipy for phyton 2.7.5, I need this version of python because my final goal it's to install class (http://class-code.net/) and montepython (http://montepython.net/).

(I have ubuntu 12.04)

When I tried to import numpy or scipy I get the following error everytime:

$ phyton2.7
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/numpy/__init__.py", line 137, in <module>
  import add_newdocs
File "/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 9, in <module>
  from numpy.lib import add_newdoc
File "/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 4, in <module>
  from type_check import *
File "/usr/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 8, in <module>
  import numpy.core.numeric as _nx
File "/usr/lib/python2.7/dist-packages/numpy/core/__init__.py", line 5, in <module>
  import multiarray
ImportError: /usr/lib/python2.7/dist-packages/numpy/core/multiarray.so: undefined        symbol: PyUnicodeUCS4_AsUnicodeEscapeString

>> import scipy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/scipy/__init__.py", line 78, in <module>
  from numpy import show_config as show_numpy_config
File "/usr/lib/python2.7/dist-packages/numpy/__init__.py", line 137, in <module>
  import add_newdocs
File "/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 9, in <module>
  from numpy.lib import add_newdoc
File "/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 4, in <module>
  from type_check import *
File "/usr/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 8, in <module>
  import numpy.core.numeric as _nx
File "/usr/lib/python2.7/dist-packages/numpy/core/__init__.py", line 5, in <module>
  import multiarray
ImportError: /usr/lib/python2.7/dist-packages/numpy/core/multiarray.so: undefined symbol: PyUnicodeUCS4_AsUnicodeEscapeString

In order to get rid of the error I had tried:

  1. Satisfy python requirement: $ sudo apt-get install build-essential python-dev gfortran
  2. Install with apt-get: $ sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose
  3. Install requirement with pip: $ sudo pip install requests
  4. … and tried to installing with pip: $ sudo pip install numpy and $ sudo pip install scipy
  5. I already have the environment var: export PYTHONPATH=/usr/lib/python2.7/dist-packages:${PYTHONPATH}
    The last time I installed numpy and scipy (in older ubuntu version 10.x) this worked for me.
  6. sudo sh -c "wget http://python-distribute.org/distribute_setup.py -O- | python" sudo sh -c "wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py -O- | python" and then install both of them with pip as in (4)

    But now it seems that the server of python-distribute.org is down. I got the following error:

    http://python-distribute.org/distribute_setup.py
    Resolving python-distribute.org (python-distribute.org)... failed: Name or service not known.
    wget: unable to resolve host address `python-distribute.org'
    
  7. I had also tried to install numpy and scipy from synaptic and Ubuntu software center

I had failed in each of this attempts and now I don't know what to do.
Am I missing something?
How can I get numpy and scipy correctly installed for python?
Any idea will be appreciated.

when I do

echo $PYTHONPATH

I get

/usr/lib/python2.7/dist-packages

Best Answer

  • Ok, lets follow the installation guide:

    1. It says you need python 2.7 (which you already have):

      $ python --version
      Python 2.7.4
      
    2. Then it says that you need the numpy package too, version >= 1.4.1:

      apt-cache policy python-numpy
      python-numpy:
      Installed: (none)
      Candidate: 1:1.7.1-1ubuntu1
      Version table:
       1:1.7.1-1ubuntu1 0
          500 http://archive.ubuntu.com/ubuntu/ raring/main amd64 Packages

      As you can see, I have available numpy version 1.7.1, so lets proceed to install it:

      sudo apt-get install python-numpy
      
    3. Now it says that we need cython, lets check if that package is availabe:

      apt-cache policy cython
      cython:
      Installed: (none)
      Candidate: 0.17.4-0ubuntu1
      Version table:
       0.17.4-0ubuntu1 0
          500 http://archive.ubuntu.com/ubuntu/ raring/main amd64 Packages

      We have it, we install it:

      sudo apt-get install cython
      

      Please, do notice that there are other packages that are dependency that are being installed too.

    4. Oddly enough, we also need the scipy module too:

      sudo apt-get install python-scipy
      
    5. Testing. Open python in a terminal and type the following:

      $ python
      Python 2.7.4 (default, Sep 26 2013, 03:20:26) 
      [GCC 4.7.3] on linux2
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import numpy
      >>> import scipy
      >>> import cython
      >>> exit()
      

      The above, has to be without errors. If something went wrong, go up and read the guide again, you forgot/skiped a step.

    6. Downloading the CLASS sources from their page, go to the directory in the terminal, and untar it and make it:

      cd where/the/tar/is
      tar zxf class_v2.0.2.tar.gz
      cd class_v2.0.2
      make

      Here we will wait until is complete. Once this is done, we go to the python directory and start the magic. Without moving from directories run this:

      cd python
      python setup.py build
      python setup.py install --user

      Now we have CLASS installed in our user directory. We should test it!

      python
      Python 2.7.4 (default, Sep 26 2013, 03:20:26)
      [GCC 4.7.3] on linux2
      Type "help", "copyright", "credits" or "license" for more information.
      >>> from classy import Class
      >>> exit()
      

      Everything is nice a cozzy until here.

    7. Now the truth time, download monthypython tarball in your code directory:

      cd ~
      mkdir code
      cd code
      wget https://www.dropbox.com/s/l7mnhwsktp1m8b5/montepython_v1.2.5.tar.bz2?dl=1 -O montepython_v1.2.5.tar.bz2
      tar jxf montepython_v1.2.5.tar.bz2

      In the installation instructions are some notes that aren't relevant in this peculiar case (a new install) but it may be important when you update montepython.

    8. This change is important. You remember where you installed CLASS? Now you should tell Monte Python where is that. In my case it is /home/braiam/src/class_v2.0.2/class, yours may be different. You can use find to look for it:

      find ~ -name class -print
      

      Well, now I modify the default.conf file using whatever you like (I'll use nano) and modify the root path, so the complete file will looks like:

      # Fill in the relevant path to your personal distribution. 
      # If you create a new file out of this one, please remember to call
      # MontePython.py with the option '-conf my.conf'
      
      # At minimum, this file should contain one line:
      # ** path['cosmo'] =  path to the cosmological code you are using. Note, if you are
      # using a modified version of class, be sure that the path contains the word
      # class, otherwise the code might not recognise it.
      
      # If you want to use a data folder different from the one present in the folder
      # you are executing the code, please also add:
      # ** path['data']  =  /path/to/the/other/data/
      
      root = '/home/braiam/src/class_v2.0.2/'
      
      path['cosmo']           = root+'/class/'
      

      Save the file and exit.

    9. Test montepython! If everything was ok, you should be able to run code/MontePython.py --help in your montepythong directory and everything should run:

      ~/src/montepython$ code/MontePython.py --help
      usage: MontePython.py [-h] [-N number of steps] [-o output folder]
                        [-p input param file] [-c input cov matrix]
                        [-j jumping method] [-f jumping factor]
                        [-conf configuration file] [-chain_number chain number]
                        [-r restart from chain] [-bf restart from best fit file]
                        [-info [compute information of desired file [compute information of desired file ...]]]
                        [-bins desired number of bins, default is 20] [-no_mean]
                        [-comp comparison folder]
                        [-extra plot file for custom needs] [-noplot] [-all]
                        [-ext change extension for the output file]
                        [-fontsize desired fontsize, default is 15]
                        [-ticksize desired ticksize, default is 13]
      
      Monte Python, a Monte Carlo code in Python

      optional arguments: -h, --help show this help message and exit -N number of steps -o output folder -p input param file -c input cov matrix -j jumping method -f jumping factor -conf configuration file -chain_number chain number -r restart from chain -bf restart from best fit file -info [compute information of desired file [compute information of desired file ...]] -bins desired number of bins, default is 20 -no_mean -comp comparison folder -extra plot file for custom needs -noplot -all -ext change extension for the output file -fontsize desired fontsize, default is 15 -ticksize desired ticksize, default is 13

    With this, you have montepython installed and ready to roll!

  • Related Question