10

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.

5
  • try to set the environment variable PYTHONPATH pointing to the folder where your numpy module is, in Windows it is C:\Python27\Lib\site-packages Commented Aug 13, 2013 at 8:34
  • Could you please let me know how to set PYTHONPATH? I am a very beginner. Thank you Commented Aug 13, 2013 at 9:05
  • try: export PYTHONPATH=path_to_site_packages... let me know if this worked... Commented Aug 13, 2013 at 9:09
  • @SaulloCastro Thank you so much! It works! It has confused me many days! Commented Aug 13, 2013 at 9:12
  • I posted it as an answer so that you can accept it... ;) Commented Aug 13, 2013 at 9:15

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you for your help. I tried to use the pip in virtualenv. It has been successfully installed (Successfully installed numpy Cleaning up...), but I till did not work when I imported numpy in virtualenv.
Can you check that a numpy directory exists in venv/lib/python2.7/site-packages/?
Also can you check the output of which python when you're in your virtualenv - it should give you something like /home/myname/myproject/venv/bin/python rather than your global python.
No, it is not there, but why it said installed successfully :(
The output of which python is "/home/kevin/python/Python-2.7.5/myproject/flask/venv/bin/python"
|
1

meddling with PYTHONPATH for site-packages indeed defeats the purpose of virtalenv. what worked for me was to specify the env i wanted the packages to be installed in via pip

example:

pip -E /home/proj1 

where proj1 was created using virtualenv.

reference: how to install numpy in a virtualenv

Comments

0

You can use python to install it

 python -m pip install numpy

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.