Forums

Correct way to install modules in virtualenv

Hi guys,

I created a virtual env and successfully installed Django 1.6 on it, and a bunch of other modules that I wanted to use in my apps. However, when launching my app, some modules I had installed did not import correctly with the error of 'not being found'.

Then I realized in the filesystem the 'pip' commands had installed the modules into python 2.6 site-packages while my app was had the paths all set to python 3.3

I saw some guy had a similar problem @ http://www.reddit.com/r/Python/comments/1qa71i/problem_with_virtualenv_django_on_python33/

I tried using the command pip3.3 thru the online bash console instead to install the modules, but the install failed by not having the admin rights to write into the python 3.3 site packages folder.

So as a work-around, I did something similar to https://www.pythonanywhere.com/forums/topic/123/#id_post_681 by adding

path = '//home/zjcheah/.virtualenvs/django16/lib/python2.7/site-packages'
if path not in sys.path:
    sys.path.append(path)

to the WSGI file ( the one from the 'Web' tab, not the WSGI in the django project.)

What's the proper way to install modules into the python 3.3 virtualenv that's being used by the app?

Make sure you actually "activate" the virtualenv, if you want to work on it. If you're using virtualenvwrapper (which it looks like you are), you would use, eg

workon django16

Then, to check which versions of pip and python will be used:

which pip  #  should show /home/zjcheah/.virtualenvs/django16/bin/pip
pip freeeze # will show the packages installed in your virtualenv
which python # should show the virtualenv python
python 
# should show you python3.3, if that's what you set as your virtualenv
>>> import django
>>> print django.__file__
# will show which version of django it's using, and where it came from

If in doubt, re-create a new virtualenv, with

mkvirtualenv django16again --python=python3

(assuming you want python 3.)

thanks harry. could we put that --python=[python version] into the wiki?

Good idea. Done.