StatsSorceress
StatsSorceress

Reputation: 3099

Python virtual environment cannot find python modules

I have both Canopy and Anaconda installed. My default is Canopy, but when I launch screen I get Anaconda. That's useful for me most of the time, but this time I need to use Canopy in screen.

I think the solution is to create a virtual environment, and I did so like this:

cd /path/to/project
virtualenv my_project

Problem: the virtualenv cannot see my installed Python modules, like numpy.

Here's the rest of my process:

user@GPU5:~/path/to/my_project$ screen
user@GPU5:~/path/to/my_project$ source activate lstm_project
(lstm_project) user@GPU5:~/path/to/my_project$ python

Enthought Canopy Python 2.7.6 | 64-bit | (default, Sep 15 2014, 17:36:10) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

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

Here is the output of sys.path:

>>> print sys.path

['', '/user/path/to/my_project', 
'/opt/enthought/canopy-1.5.1/appdata/canopy-1.5.1.2730.rh5-x86_64/lib/python27.zip', 
'/opt/enthought/canopy-1.5.1/appdata/canopy-1.5.1.2730.rh5-x86_64/lib/python2.7', 
'/opt/enthought/canopy-1.5.1/appdata/canopy-1.5.1.2730.rh5-x86_64/lib/python2.7/plat-linux2', 
'/opt/enthought/canopy-1.5.1/appdata/canopy-1.5.1.2730.rh5-x86_64/lib/python2.7/lib-tk', 
'/opt/enthought/canopy-1.5.1/appdata/canopy-1.5.1.2730.rh5-x86_64/lib/python2.7/lib-old', 
'/opt/enthought/canopy-1.5.1/appdata/canopy-1.5.1.2730.rh5-x86_64/lib/python2.7/lib-dynload', 
'/user/path/to/my_project/lib/python2.7/site-packages']

How can I get my virtualenv to recognize the packages I have installed globally?

Upvotes: 1

Views: 4291

Answers (2)

Gabriel Belini
Gabriel Belini

Reputation: 779

Working on several projects in the same machine might cause conflicts between some Python Packages that are used in a project but aren't in other ones.

Thats the main reason people use virtualenvs.

What happens is, you create one environment for each one of your projects and install the dependencies only inside that environment, doing this will avoid package conflicts and this is also a very good way of keeping track of which packages each one of your project needs to run, so if someone else (or even you) wants to run a project in another computer, its very easy to install all needed packages (and nothing more) to run it.

To answer your question, if you want to install all of your global packages inside a virtual environment do the following:

  1. Open a terminal (deactivate virtualenv if you're inside one);
  2. Execute: pip freeze > requirements.txt (this will create a .txt file with all of your global python packages);
  3. Activate the environment that you want to install your packages;
  4. Execute pip install -r requirements.txt (make sure you're in the same folder as requirements.txt.

I hope that I didn't confuse you too much, if you have any questions feel free to ask.

Upvotes: 2

Jason Chen
Jason Chen

Reputation: 194

virtualenv by default will ignore system packages. When you're creating the environment, do

$ virtualenv my_project --system-site-packages

Upvotes: 2

Related Questions