Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install python-numpy in the Virtualenv environment

I would like to install the python-numpy in the Virtualenv environment. My system is Ubuntu 12.04, and my python is 2.7.5. First I installed the Virtualenv by

$ sudo apt-get install python-virtualenv

And then set up an environment by

$ mkdir myproject
$ cd myproject
$ virtualenv venv
New python executable in venv/bin/python
Installing distribute............done.

Activated it by

$ . venv/bin/activate

Installed python-numpy in the environment by

$ sudo apt-get install python-numpy

However, I tried to import numpy in python in the environment after all steps above. Python told me "No modules named numpy". Whereas, numpy could be imported in Python globally. I tried to remove and install many times but it does not work. I am a beginner of both Python and Linux.

like image 752
user2677756 Avatar asked Aug 13 '13 08:08

user2677756


1 Answers

apt-get will still install modules globally, even when you're in your new virtualenv.

You should either use pip install numpy from within your virtual environment (easiest way), or else compile and install numpy from source using the setup.py file in the root of the source directory (slightly harder way, see here).

I'd also thoroughly recommend you take a look at virtualenvwrapper, which makes managing virtual environments much friendlier.

Edit:

You should not be using sudo, either to create your virtual environment or to install things within it - it's a directory in your home folder, you don't need elevated permissions to make changes to it. If you use sudo, pip will make changes to your global site packages, not to your virtual environment, hence why you weren't able to install numpy locally.

Another thing to consider is that by default, new virtualenvs will inherit from the global site-packages - i.e. if Python can't find a module locally within your virtualenv, Python will also look in your global site packages *. In your case, since you'd already installed numpy globally (using apt-get), when you then try to pip install numpy in your virtual environment, pip sees that numpy is already in your Python path and doesn't install it locally.

You could:

  1. Pass the --no-site-packages option when you create your virtualenv. This prevents the new virtualenv from inheriting from the global site packages, so everything must be installed locally.

  2. Force pip to install/upgrade numpy locally, e.g. using pip install -U --force numpy


* As of v1.7, the default behaviour of virtualenv is to not include the global site-packages directory. You can override this by passing the --system-site-packages flag when creating a new virtual environment.

like image 88
ali_m Avatar answered Nov 22 '22 05:11

ali_m