WebFaction
Community site: login faq
5
4

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 27 Jun '11, 10:53

philgyford's gravatar image

philgyford
65138
accept rate: 0%

edited 27 Jun '11, 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.

# 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}
easy_install-${PYTHONVER} pip
pip install --user virtualenv
pip install --user virtualenvwrapper

# 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:

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:

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.

permanent link
This answer is marked "community wiki".

answered 28 Sep '12, 04:42

ryans's gravatar image

ryans ♦♦
5.0k42552
accept rate: 43%

edited 19 Jun '14, 14:16

bmeyer71's gravatar image

bmeyer71 ♦♦
1.4k2613

This rocks! Thanks Ryan!!! :)

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

WOW. This worked perfectly. THANK YOU.

(28 Sep '12, 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.

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

For me, the wget command was causing an error because of a certificate mismatch. You'll probably need to use this instead: wget 'http://pypi.python.org/packages/source/v/virtualenvwrapper/virtualenvwrapper-4.0.tar.gz' --no-check-certificate

Could someone with more clout than I update the answer?

(02 Aug '13, 09:21) Josh Josh's gravatar image

Creating my own sitecustomize.py file was exactly what I needed. Thank you Ryan! One thing though, for me the global sitecustomize.py was at this location: /usr/local/lib/pythonX.Y/sitecustomize.py

(24 Dec '13, 12:34) pymarco pymarco's gravatar image

If you are using pip version 1.5 or newer you will need to add --use-no-wheel when installing the packages or you will get an error. For example:

pip3.3 install virtualenv --no-use-wheel
(04 Feb '14, 15:44) timg ♦♦ timg's gravatar image

+1 to @timg's last comment. I'm trying to setup Python3 with pip and virtualenv/virtualenvwrapper. I had to pip uninstall virtualenv and virtualenvwrapper and then re-install both using:

$ pip3 install virtualenv --no-use-wheel
$ pip3 install --install-option="--user" virtualenvwrapper --no-use-wheel

Found exact code via this gist.

(06 Jan '15, 01:48) mhulse mhulse's gravatar image

Note to future readers: If you are using Python 3.4.x and above, check out Python's native pyvenv! It's a bit easier than virtualenv/wrapper. If it's of any help, I've posted some of my pyvenv notes here. Oh, and pyvenv comes with Pip, so you don't have to easy_install!!!! Life is beautiful. :)

(10 Jan '15, 06:51) mhulse mhulse's gravatar image

Thank you very much! Your post was very helpful in successfully upgrading my own WebFaction Django projects to work under virtualenv. I wrote up my experience here: http://stationinthemetro.com/2015/07/31/upgrading-django-to-use-virtualenv-on-webfaction

(01 Aug '15, 02:05) bobtiki bobtiki's gravatar image

I followed these instructions and now have a virtualenv in /home/myuser/.virtualenvs

I set up a wsgi app from the Webfaction control panel.

I assume I put my Django app in a virtualenv under /home/myuser/.virtualenvs ...right?

But what I see in all of the httpd.conf and wsgi examples from people who have used those same virtualenv instructions (like bobtiki, whose very nicely documented article, I am also trying to follow) is paths like: WSGIPythonPath /home/myuser/webapps/new_django:/home/myuser/webapps/new_django/test_django:/home/myuser/webapps/new_django/lib/python2.7 where it looks like bobtiki put his django app under his wsgi app directory, not in the virtual env.

And the official Webfaction instructions use examples like /home/demo/webapps/mywsgiapp/venv/ where the venv is under the wsgi app.

I am confused. I am probably just missing something. Might even be obvious. Wouldn't be the first time...

(31 May '16, 20:04) bhaugen bhaugen's gravatar image

After more study, it looks like that is what bobtiki and several other people, including philgyford, do. The wsgi file says that the site-packages live in the virtualenv. So I guess the django app finds them there?

(01 Jun '16, 20:22) bhaugen bhaugen's gravatar image

Finally got my stuff up and running. My django app is still deployed under webapps/django, but now using the site packages in the virtualenv. These instructions worked fine: https://docs.webfaction.com/software/mod-wsgi.html?highlight=virtualenv#using-a-virtual-environment-with-mod-wsgi

I did not need to change the wsgi file.

(02 Jun '16, 11:34) bhaugen bhaugen's gravatar image
showing 5 of 12 show 7 more comments

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:

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

Then:

$ easy_install virtualenv
$ easy_install pip
$ pip install virtualenvwrapper
$ mkdir ~/.virtualenvs
$ vim ~/.bashrc

Add these lines at the end of .bashrc:

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:

$ 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:

(my_env_name)$ pip install yolk

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

(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...

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:

(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.

permanent link

answered 27 Jun '11, 12:38

philgyford's gravatar image

philgyford
65138
accept rate: 0%

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')

(14 Dec '11, 04:57) sergio morst... 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.

(28 May '12, 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.

(28 May '12, 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?

(11 Jun '12, 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.

(11 Jun '12, 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?

(11 Jun '12, 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.

(11 Jun '12, 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"

(12 Jun '12, 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.

(12 Jun '12, 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!

(12 Jun '12, 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)

/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
(12 Jun '12, 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.

(12 Jun '12, 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

(28 Sep '12, 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.

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

the lines

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.

(15 Feb '13, 08:50) OpenDCU OpenDCU's gravatar image
showing 5 of 15 show 10 more comments

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 :)

permanent link

answered 27 Jun '11, 11:15

David%20L's gravatar image

David L ♦♦
1.4k13
accept rate: 45%

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.

(16 Jan '12, 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.

permanent link

answered 27 Jan '12, 20:41

gfxcomplex's gravatar image

gfxcomplex
725
accept rate: 0%

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.

(27 Jan '12, 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.

(27 Jan '12, 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):

$ 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):

# 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.
(06 Mar '12, 02:45) mhulse mhulse's gravatar image

In the accepted answer, I think:

pip install virtualenv

should instead be

pip install --user virtualenv
permanent link

answered 08 Jun '14, 02:44

shafiquejamal's gravatar image

shafiquejamal
111
accept rate: 0%

I had some troubles figuring out how to deploy a Python 3.4 & Django 1.8 app in Webfaction using virtual environment (Python 3.4 venv).

Basically the answer is to create the application in Webfaction control site as a mod_wsgi application, set up Django and virtual environment manually over SSH and modify slightly the default Apache httpd.conf. I posted my notes on my blog: http://www.mikkohaapanen.com/deploying-django-app-in-webfaction-with-virtual-environment/

permanent link

answered 08 Nov '15, 09:59

mikko2015's gravatar image

mikko2015
111
accept rate: 0%

Your answer
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

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×871
×48

question asked: 27 Jun '11, 10:53

question was seen: 26,799 times

last updated: 02 Jun '16, 11:34

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2016 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM