WebFaction
Community site: login faq
1
1

I've been reading through this thread http://community.webfaction.com/questions/4253/simple-virtualenv-usage-with-django and successfully set up a virtualenv, however, now I'm wondering how I should use this when my websites/applications are all set up through the GUI on the webfaction site.

I'm basically new to Django/python and am trying to use best practices. VirtualEnvs seems to be the best first step. Now that I have a virtualenv set up, don't I need to create a new Django project within this VE? If so, how do I wire that up to my domain through webfaction?

asked 28 Sep '12, 09:12

vegan's gravatar image

vegan
31111
accept rate: 50%


in your wsgi file you have to activate your virtualenv. That's it!

In your webapp project folder find the wsgi file

add this to your wsgi file, something like this

import os, sys, site

site.addsitedir('/home/ACCOUNT_NAME/PathtoMyYourVirtualenv/lib/python2.7/site-packages')

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

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

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

sys.path = ['/home/ACCOUNT_NAME/webapps/myapp/myapp', '/home/ACCOUNT_NAME/webapps/myapp/any_otherPaths?', '/home/ACCOUNT_NAME/webapps/myapp'] + sys.path

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

answered 21 Dec '12, 08:39

Prajwal's gravatar image

Prajwal
10611430
accept rate: 33%

This is the correct answer. It is elegant and simple. Nice job, Prajwal. :)

(10 Sep '13, 11:48) likebike likebike'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

Since there is no way to create virtualenvs from the control panel you can't really 'wire' them up to the Control Panel.

I am sure there are many ways to do this. The way that I personally do it is to create one mod_wsgi3.x/python2.x application which provides you with an apache/wsgi instance only. Then I have a .virtualenvs directory in my home directory that I keep my projects in. Then I use virtualhosts in my httpd.conf to point to the projects.

The only thing you need to do after this is create the applications to point to your static media files for the individual projects. This should be done whichever way you go so that they are served by the front end nginx server which is faster for this type of media.

The benefit of this is that you only have one apache instance consuming your account memory and only one place that you need to go to make any changes.

I initially started with this as a guide. So maybe take a look at that to help get some ideas on how you would like to structure your setup.

permanent link

answered 28 Sep '12, 12:01

bmeyer71's gravatar image

bmeyer71 ♦♦
1.4k2613
accept rate: 33%

Thanks, @bmeyer. I'm definitely having issues with static files as well.

(28 Sep '12, 12:46) vegan vegan's gravatar image

If there are details you can provide here, please do. Otherwise you can open a support ticket for us to look at your account closer.

(28 Sep '12, 12:52) bmeyer71 ♦♦ bmeyer71's gravatar image

As far as getting the static files served, I'm still seeing an admin page with no CSS. This is with the plain Django installation, not a new one I rewired. I would think this would just work out of the box.

I've tried several different methods to fix the Static URL so the CSS will show up, but so far nothing has worked.

(28 Sep '12, 13:02) vegan vegan's gravatar image

You will want to take a look at our docs here for serving static/admin media. This will serve the media from our frontend nginx server.

(28 Sep '12, 13:11) bmeyer71 ♦♦ bmeyer71's gravatar image

Following those directions worked. Thank you!

-- I spoke too soon, it's broken again. I restarted the apache server and now the css is gone again and now the root directory of my site does not work.

(28 Sep '12, 13:27) vegan vegan's gravatar image

Excellent. You are very welcome.

Check your settings.py for your STATIC_ROOT/STATIC_URL. It looks like you have a full directory path specified for the url.

(28 Sep '12, 13:39) bmeyer71 ♦♦ bmeyer71's gravatar image

I changed the STATIC_URL to "/static/" instead of "http://mydomain.com/static/", this would force me to change it to "localhost..." every time I worked locally.

It looks like if I remove the admin path from urls.py, the home page works, but not the admin, and vice versa if I add it back in.

(28 Sep '12, 14:26) vegan vegan's gravatar image

Your admin was loading fine. Just your home page had a template error. Try specifying the full path to the template directory in your settings.py instead of using a variable.

(28 Sep '12, 15:04) bmeyer71 ♦♦ bmeyer71's gravatar image

I am hitting this issue when trying to do this: https://github.com/pypa/virtualenv/issues/4 I can't create the virtualenv. I have pip install --upgraded virtualenv and readline to no avail. I get AssertionError: Filename /home/anentropic/lib/python2.7/readline.so does not start with any of these prefixes: ['/usr/local']

(18 Dec '12, 21:36) anentropic anentropic's gravatar image

anentropic: Send us a support ticket so we can look at your server environment.

(19 Dec '12, 02:25) johns ♦♦ johns's gravatar image
showing 5 of 10 show 5 more comments
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:

×877
×50

question asked: 28 Sep '12, 09:12

question was seen: 5,046 times

last updated: 01 Aug '15, 02:05

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