1

dI have installe numpy on ubuntu by executing

sudo apt-get install python-numpy

while executing on terminal I get this error.

>>> import numpy as np
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>> 

why this is happening? I tried many a times by going through internet but I couldnt find a solution.Could you please tell me how to solve this?

Edit1: I came to know that I have to install numpy for the python version which I run on terminal, using pip.. Python 2.7.3 runs when I enter 'python' on terminal. So that means I have to install numpy for python 2.7.3. Can someone guide me how to do it? I couldnt figure it out by myself. BTW I am using Ubuntu 12.04 if that helps.

Edit 2: I did some more digging into this.. my /usr/lib contains two directories python2.7 and python3.While Python2.7 directory consists of a large number of files and sub directories,python3 directory has only dist-packages subdirectory which consists of deb_conf.py anf lsb_release.py..I think I tried python3 few months back and then removed it..But right now python2.7 is the only thing i am having.

Edit 3:

SO here are the outputs of the commands you asked me to enter

~$ virtualenv --no-site-package -p /usr/bin/python2.7 my_env
Running virtualenv with interpreter /usr/bin/python2.7
The --no-site-packages flag is deprecated; it is now the default behavior.
New python executable in my_env/bin/python2.7
Not overwriting existing python script my_env/bin/python (you must use my_env/bin/python2.7)
Installing  distribute..............................................................................................................................................................................................done.
~$ source my_env/bin/activate
~$ pip install numpy

last command gave a generated a lot of logs which ended with something like this..

Creating build/scripts.linux-i686-2.7/f2py2.7
  adding 'build/scripts.linux-i686-2.7/f2py2.7' to scripts
changing mode of build/scripts.linux-i686-2.7/f2py2.7 from 664 to 775

changing mode of /home/sandeep/my_env/bin/f2py2.7 to 775
Successfully installed numpy
Cleaning up...

After all these I tried to run python again and this is the output.

~$ python
Python 2.7.3 (default, Jan 20 2013, 21:40:19) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>> 
sandeep p
  • 121
  • 1
  • 12
  • 3
    You likely have multiple versions of Python on your system. – mechanical_meat Apr 05 '13 at 15:52
  • I believe you can use pip to install packages whilst specifying the version. – mechanical_meat Apr 05 '13 at 15:53
  • 1
    Yes, in fact, you can make use of pip to install numpy to the Python version of your choosing. More here: http://stackoverflow.com/a/10919761/42346 – mechanical_meat Apr 05 '13 at 15:54
  • @BrianCain output of which python is this /usr/local/bin/python – sandeep p Apr 05 '13 at 18:53
  • @bernie It is possible that I have multiple versions of python in my system.. But How do I make sure that? Also when I run python on terminal it shows 'Python 2.7.3 (default, Jan 20 2013, 21:40:19)' – sandeep p Apr 05 '13 at 18:56
  • Determining which versions you have installed doesn't, in my opinion, help you get to your end-goal. Is there a specific reason you do not wish to use pip to install numpy? – mechanical_meat Apr 05 '13 at 19:22
  • @bernie I do not mind using pip to install numpy. But I couldnt figure out how to do it. – sandeep p Apr 05 '13 at 19:29
  • I did some more digging into this.. and my /usr/lib contains two folder python2.7 and python3.While Python2.7 directory consists of a large number of files and sub directories,python3 directory has only dist-packages subdirectory which consists of deb_conf.py anf lsb_release.py..I think I tried python3 few months back and then removed it..But right now python2.7 is the only thing i am having. – sandeep p Apr 05 '13 at 21:02
  • Just in case you didn't understand @BrianCain. Just run `which python` to know which python you're in fact running. This will help you know at least if there is an other python somewhere else. – Loïc Faure-Lacroix Apr 05 '13 at 21:08
  • Another easy workaround is uninstall python completely ;) and install Enthought Python which has everything you need (scipy, numpy, matplotlib, etc). http://www.enthought.com/products/epdgetstart.php?platform=linux – yasouser Apr 05 '13 at 21:09
  • @yasouser I would not necessarily recommend uninstalling Python because of the possibility that other programs on your system could be using it. – mechanical_meat Apr 05 '13 at 21:09
  • @yasouser Not a good idea. Better to install virtualenv with no-sites-packages to get everything isolated from the OS python. Removing python is pointless since some programms do really use it. – Loïc Faure-Lacroix Apr 05 '13 at 21:15
  • @bernie and Loïc Faure-Lacroix: I recommended to reinstall a different flavour of Python (Enthought Python) which bundles all the modules he is looking for. Enthought Python comes with Python v2.7.3. I'm not asking to remove Python altogether. – yasouser Apr 05 '13 at 22:09
  • @yasouser By uninstalling python, It will break dependencies and force the package manager to remove some critical part of the system. Installing Enthought python alongside the current won't do any harm and should just work. Even if you wanted to replace it, the package manager will only see `deleted`. And there is no problem with having multiple version of python2.7 binaries on the same machine. – Loïc Faure-Lacroix Apr 06 '13 at 00:27
  • @LoïcFaure-Lacroix: Thanks for the explanation. It makes sense. I think your suggestion of `virtualenv` will be very helpful for the OP. – yasouser Apr 06 '13 at 18:17
  • Output of `apt-cache policy python-numpy`? – Gayan Weerakutti Jan 07 '18 at 13:37

1 Answers1

3

In case nothing works.

  1. Install python-virtualenv if it's not yet done.
  2. Create a virtual env

    virtualenv name

  3. Start the virtualenv

    source name/bin/activate

  4. Install numpy with easy_install or pip

  5. Profit

Note:

Virtualenv activation has to be done everytime. But you can make that task easier with virtualenv wrapper.

http://virtualenvwrapper.readthedocs.org/en/latest/

There are a lot of reasons to use virtualenv instead of ubuntu packages. In some way, I recommend not touching as much as possible the "OS" python. And if you need it for a project, use virtualenv. Python in virtualenv won't mess with other apps and you don't have to use sudo to install new packages.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
Loïc Faure-Lacroix
  • 13,220
  • 6
  • 67
  • 99
  • Thanks for your reply.. I did try this. Didn't work for me.But the thing is I want to work in Python 2.7 which is the default version that comes with Ubuntu 12.04. As I mentioned in my original post I don't believe that my system has another python version. Do you have any other ideas? I would be grateful to you... – sandeep p Apr 05 '13 at 21:33
  • 1
    What did not work? Can you post what you did and where it failed? Also in any case, run `virtualenv` like that: `virtualenv --no-site-packages {name}`, And you can specify it to install a virtualenv using python27 like that `virtualenv --no-site-package -p /usr/bin/python2.7 name` for example – Loïc Faure-Lacroix Apr 05 '13 at 21:38
  • okay.. Got it..python should be run from virtual environment my_env right? I opened another terminal and tried to run.. Thats why it was giving errors.. Thank you so much!!! – sandeep p Apr 05 '13 at 22:12
  • Yes, if you open a different terminal it won't get activated by itself. You can for example add a line like this in your `.bashrc` file. `source your_env/bin/activate`. After that, every new terminal will start with this env activated. – Loïc Faure-Lacroix Apr 06 '13 at 00:29