login community faq
1
3

I've been through the Webfaction 'Getting Started with Django' instructions which were very clear and make sense.

However, I have an existing Django project I wish to use, and I'd like to run it within a virtualenv and I'm not sure how to amend those instructions for running the server within a virtualenv. I have virtualenv running OK on the command line, but hit problems when trying to access my Django site.

I've read these instructions which helped me get this far (using a standard Django app, as per the above instructions, rather than a mod_wsgi one), but Django seems confused about project and app names, and I'm stuck.

Does anyone have simple instructions, to continue from Webfaction's Django instructions, about using virtualenv?

asked Jun 27 '11 at 10:53

philgyford's gravatar image

philgyford
2317

edited Jun 27 '11 at 10:54


Virtualenv has experienced some changes since this question was originally written. Also, some people like to remove the packages in $HOME/lib/pythonX.Y from their virtualenvs, and some do not. I'm taking all of the information presented (thanks to philgyford and mhulse), updating it, and organizing it to include both options.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# First, set your python version. I assume python2.7 in this example.
PYTHONVER=2.7

# Install virtualenv and virtualenvwrapper
PYTHON=python${PYTHONVER}
mkdir -p $HOME/{bin,tmp,lib/$PYTHON,src}
easy_install-${PYTHONVER} pip
pip install virtualenv

cd $HOME/src
wget 'http://pypi.python.org/packages/source/v/virtualenvwrapper/virtualenvwrapper-3.6.tar.gz'
tar -xzf virtualenvwrapper-3.6.tar.gz
cd virtualenvwrapper-3.6
PYTHONPATH=$HOME/lib/$PYTHON $PYTHON setup.py install --home=$HOME

# Update $HOME/.bashrc with appropriate environment variables
echo 'export PATH="$HOME/bin:$PATH"' >> $HOME/.bashrc
echo 'export TEMP="$HOME/tmp"' >> $HOME/.bashrc
echo "alias python=${PYTHON}" >> $HOME/.bashrc
echo "export PYTHON=${PYTHON}" >> $HOME/.bashrc
echo 'export WORKON_HOME="$HOME/.virtualenvs"' >> $HOME/.bashrc
echo 'export VIRTUALENVWRAPPER_TMPDIR="$WORKON_HOME/tmp"' >> $HOME/.bashrc
echo "export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/$PYTHON" >> $HOME/.bashrc
echo 'source $HOME/bin/virtualenvwrapper.sh' >> $HOME/.bashrc
echo 'export PIP_VIRTUALENV_BASE=$WORKON_HOME' >> $HOME/.bashrc
echo 'export PIP_RESPECT_VIRTUALENV=true' >> $HOME/.bashrc

# load these changes
source $HOME/.bashrc
hash -r

Now that virtualenv and virtualenvwrapper are installed, you can create, activate, deactivate, and remove environments:

1
2
3
4
mkvirtualenv foo    # Create virtualenv "foo" in $WORKON_HOME
workon foo          # Activate virtualenv "foo"
deactivate          # Deactivate current virtualenv
rmvirtualenv foo    # Remove virtualenv "foo"

This virtualenv will not use the global site packages in /usr/local/pythonX.Y/site-packages . This is because, as of virtualenv 1.7, the --no-site-packages option is active by default.

However, because of the global sitecustomize.py file at /usr/local/pythonX.Y/sitecustomize.py, the packages inside of your $HOME/lib/pythonX.Y directory are added to your python path, even inside of a virtualenv.

In some cases this is what you want and is convenient. Other times, you do not want a particular virtualenv to use your home-installed packages. If that's the case, then create an empty sitecustomize.py file inside of the virtualenv itself, which will act to override the global one and prevent the home-installed packages from being added to your path:

1
2
3
4
mkvirtualenv foo
deactivate
touch $WORKON_HOME/foo/lib/$PYTHON/sitecustomize.py
workon foo

and then this virtualenv ("foo") will not use the python packages in $HOME/lib/pythonX.Y.

answered Sep 28 '12 at 04:42

ryans's gravatar image

ryans ♦♦
28011420

edited Dec 23 '12 at 18:19

This rocks! Thanks Ryan!!! :)

(Sep 28 '12 at 05:06) mhulse mhulse's gravatar image

WOW. This worked perfectly. THANK YOU.

(Sep 28 '12 at 08:57) vegan vegan's gravatar image

I'm marking this as the accepted answer because the others are outdated now and causing some confusion.

(Jan 06 at 21:41) ryans ♦♦ ryans's gravatar image

Oh, thanks David. I found a couple of crucial wsgi configuration lines in there that eventually got my site working! For future visitors, below is a summary of everything I've done to get things working. If anyone has any suggestions about things to improve, do let me know, as I've only worked this out by trial and error. This assumes you've probably used pip and virtualenv/virtualenvwrapper before and just want to get it working on WebFaction.

Follow the 'Getting Started with Django' instructions for setting up a Django site. This will have created:

1
2
/home/username/webapps/django_application/myproject/
/home/username/webapps/django_application/myproject.wsgi

Then:

1
2
3
4
5
$ easy_install virtualenv
$ easy_install pip
$ pip install virtualenvwrapper
$ mkdir ~/.virtualenvs
$ vim ~/.bashrc

Add these lines at the end of .bashrc:

1
2
3
4
export WORKON_HOME=$HOME/.virtualenvs
source /home/username/bin/virtualenvwrapper.sh
export PIP_VIRTUALENV_BASE=$WORKON_HOME # Tell pip to create its virtualenvs in $WORKON_HOME.
export PIP_RESPECT_VIRTUALENV=true # Tell pip to automatically use the currently active virtualenv.

Then:

1
2
$ source ~/.bashrc
$ mkvirtualenv --no-site-packages --distribute my_env_name

Obviously, with a meaningful name instead of 'my_env_name'. Now we're working within that virtual environment.

Install any things you need. eg:

1
(my_env_name)$ pip install yolk

Or install from a pip requirements file from an existing project:

1
(my_env_name)$ pip install -r REQUIREMENTS.txt

Then edit ~/webapps/django_application/myproject.wsgi. I don't know if all of these are required, but it seems to work for me...

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import os, sys, site

# Tell wsgi to add the Python site-packages to its path. 
site.addsitedir('/home/username/.virtualenvs/my_env_name/lib/python2.4/site-packages')

os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'

activate_this = os.path.expanduser("~/.virtualenvs/my_env_name/bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))

# Calculate the path based on the location of the WSGI script
project = '/home/username/webapps/django_application/myproject/'
workspace = os.path.dirname(project)
sys.path.append(workspace)

from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()

Obviously, replace 'username', 'my_env_name', 'django_application' and 'myproject' as appropriate.

Finally:

1
2
3
(my_env_name)$ cd ~/webapps/django_application/myproject/
(my_env_name)$ python manage.py syncdb
(my_env_name)$ ./../apache2/bin/restart

You'll need to do that last line every time you make a change to myproject.wsgi.

answered Jun 27 '11 at 12:38

philgyford's gravatar image

philgyford
2317

thanks for these instructions.

I had to make just a couple of changes to use python2.7:

mkvirtualenv --no-site-packages --distribute --python=/usr/local/bin/python2.7 my_env_name

site.addsitedir('/home/username/.virtualenvs/my_env_name/lib/python2.7/site-packages')

(Dec 14 '11 at 04:57) sergio morstabilini sergio%20morstabilini's gravatar image

Hi, sorry for coming back to this post but I have a question. I have a running installation in production. Instead of installing the django modules (e.g. dango-avatar) again, can't I just copy them or use them from my existing installation? I have made some slight modification in some modules to work with django 1.4 and I am not sure if I want to risk it and redo those changes while in production mode.

(May 28 '12 at 10:45) xpanta xpanta's gravatar image

Hi,

As long as make sure the path where they're copied is consistent with the Python search path in your virtualenv, you should be able to just copy them.

(May 28 '12 at 10:59) todork todork's gravatar image

Thank you. One last question. Do I need to completely shut down the server while doing this procedure? Or as long as everything is done according to the tutorial above the restart at the end is enough?

(Jun 11 '12 at 06:36) xpanta xpanta's gravatar image

Hi again,

Yes, you can just restart at the end, and it should be ok. If you experience any issues, feel free to open a ticket.

(Jun 11 '12 at 06:49) todork todork's gravatar image

Thank you. I will try first by myself, and see what I can do :-). Just one more question. Do I need to install Django 1.4, too, to my new virtualenv?

(Jun 11 '12 at 07:36) xpanta xpanta's gravatar image

You probably want to do that, as it's the point of using a virtualenv. Just putting Django1.4 somewhere on the Python search path would also do though.

(Jun 11 '12 at 07:49) todork todork's gravatar image

because I had pip aliased to /home/xpanta/bin/pip I accidentally installed Django 1.4 on my home path (/home/xpanta/lib/python2.7/). I actually wanted to install in my virtual-env.

What should I do now?

I tried to install it my virtual-env and I get "Requirement already satisfied (use --upgrade to upgrade): Django in /home/xpanta/lib/python2.7"

(Jun 12 '12 at 01:31) xpanta xpanta's gravatar image

You can run pip install remove to remove the package and than pip install within the virtualenv and it should work.

(Jun 12 '12 at 02:20) johns ♦♦ johns's gravatar image

If, for some reason, pip install remove does not work, you can remove Django from /home/xpanta/lib/python2.7/ manually. .egg files would be listed in the easy-install.pth file in that directory, and you would simply remove the .egg line and delete the .egg file. If you have a basic directory, like /home/xpanta/lib/python2.7/django, you can just delete it.

Once django is gone, python2.7 -c 'import django' from the command line in your home directory should no longer import any django module. (giving an ImportError exception instead). At that point, you can just install Django again using the correct pip.

Hope that helps!

(Jun 12 '12 at 02:22) ryans ♦♦ ryans's gravatar image

Isn't that dangerous for my other django projects that are not in virtualenvs? (btw, pip install remove did not work)

1
2
3
4
5
/home/xpanta/bin/pip install remove Django
Downloading/unpacking remove
  Could not find any downloads that satisfy the requirement remove
No distributions at all found for remove
Storing complete log in /home/xpanta/.pip/pip.log
(Jun 12 '12 at 03:19) xpanta xpanta's gravatar image
1

It shouldn't be dangerous, but it could be. Specifically, Django applications when set up via a one-click installer use a separate Django installed in ~/webapps/app_name/lib/python2.7, which is a separate python library directory specific to the individual application. So, by default, the Django in ~/lib/python2.7 is never needed and would only appear there if you had installed it there manually.

However, if you have set up Django applications to use this library specifically, then yes, it would be dangerous.

(Jun 12 '12 at 04:10) ryans ♦♦ ryans's gravatar image

I'm getting this error when I try installing the virtualenvwrapper:

$ pip install virtualenvwrapper Traceback (most recent call last): File "/home/vegan/bin/pip", line 7, in ? sys.exit( File "/usr/local/lib/python2.4/site-packages/pkg_resources.py", line 277, in load_entry_point return get_distribution(dist).load_entry_point(group, name) File "/usr/local/lib/python2.4/site-packages/pkg_resources.py", line 2180, in load_entry_point return ep.load() File "/usr/local/lib/python2.4/site-packages/pkg_resources.py", line 1913, in load entry = import(self.module_name, globals(),globals(), ['name']) File "/home/vegan/lib/python2.4/pip-1.2.1-py2.4.egg/pip/init.py", line 9, in ? from pip.basecommand import command_dict, load_command, load_all_commands, command_names File "/home/vegan/lib/python2.4/pip-1.2.1-py2.4.egg/pip/basecommand.py", line 4, in ? from pkgutil import walk_packages

(Sep 28 '12 at 08:47) vegan vegan's gravatar image

Please open a ticket at our support site, and we'll help you find the cause of the issue.

(Sep 28 '12 at 10:13) timg ♦♦ timg's gravatar image

the lines

1
2
activate_this = os.path.expanduser("~/.virtualenvs/my_env_name/bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))

in the wsgi.py file are the important ones. They ensure that when the server restarts after a reboot (for example) the virtualenv is activated. If you forget this (as I recently did) the app will fail to start; the error log (in ~/logs/user/error_appName.log ) will report that some module could not be found.

(Feb 15 at 08:50) OpenDCU OpenDCU's gravatar image
showing 5 of 15 show all

They are a little old, but these instructions on our forums should still work perfectly. After following our documentation you should already have most of it done :)

answered Jun 27 '11 at 11:15

David%20L's gravatar image

David L ♦♦
132113

Ugh. I'm not nearly smart or dedicated enough to follow those instructions.

I develop on a Linux box, each python project has its own virtualenv. It sure would be nice if I could just ship that entire mess up to webfaction, and know it is running in production the same way it runs in test.

This would also make the management of libraries much easier. I could finally type "yolk --list" and get the same set of libraries the wsgi app sees.

(Jan 16 '12 at 01:27) brycenesbitt brycenesbitt's gravatar image

I'm trying to learn along with you guys and when I add the paths to my .bashrc and sourced it I get this error.

Traceback (most recent call last): File "<string>", line 1, in <module> ImportError: No module named virtualenvwrapper.hook_loader virtualenvwrapper.sh: There was a problem running the initialization hooks. If Python could not import the module virtualenvwrapper.hook_loader, check that virtualenv has been installed for VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2.7 and that PATH is set properly.

answered Jan 27 '12 at 20:41

gfxcomplex's gravatar image

gfxcomplex
74

We would have to see the exact lines you are entering. You may submit a support ticket if you would like us to look in real-time.

(Jan 27 '12 at 20:52) johns ♦♦ johns's gravatar image

Thanks John, I didnt want to use up support time of webfaction since I'm learning and not so much doing it for a client but if you think that's best I will give it a go.

(Jan 27 '12 at 20:55) gfxcomplex gfxcomplex's gravatar image

Thanks for this post, it's been very helpful.

Here's some of what worked for me (after following these instructions):

1
2
3
$ easy_install-2.7 pip
$ pip install virtualenv
$ pip install virtualenvwrapper

It seemed to make more sense to use pip to install virtualenv/wrapper... Maybe there's a good reason to use easy_install?

Also, not sure if this will help others who get the "No module named virtualenvwrapper.hook_loader ..." error, but this fixed it for me (relevant contents of my .bashrc):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# User specific aliases and functions:

export PATH=$HOME/bin:$PATH

# http://docs.webfaction.com/software/python.html#error-permission-denied-during-package-installation
export TEMP=$HOME/tmp

# http://community.webfaction.com/questions/4253/simple-virtualenv-usage-with-django
# http://spyderspace.wordpress.com/2012/02/17/using-pip-and-virtualenv-on-webfaction/
alias python=python2.7
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_TMPDIR=$HOME/.virtualenvs/tmp
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2.7
source $HOME/bin/virtualenvwrapper.sh
export PIP_VIRTUALENV_BASE=$WORKON_HOME # Tell pip to create its virtualenvs in $WORKON_HOME.
export PIP_RESPECT_VIRTUALENV=true # Tell pip to automatically use the currently active virtualenv.
(Mar 06 '12 at 02:45) mhulse mhulse's gravatar image
Your answer
If you have an answer to the above question, then use the form below. Otherwise, use the appropriate 'add new comment' button above to post your feedback.
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×641
×26

Asked: Jun 27 '11 at 10:53

Seen: 7,927 times

Last updated: Feb 15 at 08:50

Plans & prices    Sign up    Why WebFaction?    Contact us    Affiliate program    Support    Legal    Jobs    Blog    Control panel login
Powered by OSQA
© Copyright 2003-2012 Swarma Limited - WebFaction is a service of Swarma Limited