WebFaction
Community site: login faq
1
1

Hi, will you please advise me on how to correctly activate virtualenv via wsgi file using Python 3.4. I still receive ImportError when loading django module which makes me believe that virtualenv is not properly activated.

I followed this thread but without any success: https://community.webfaction.com/questions/10804/using-virtualenvs-with-django

It should be something like this but for python 3.4:

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

This is how my wsgi file looks like:

import os

activate_this = '/home/user_name/webapps/app_name/env/bin/activate_this.py'
exec(open(activate_this).read())

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app_name.settings.develop")

application = get_wsgi_application()

Thanks, Jakub

asked 22 Dec '15, 17:35

jakub_l's gravatar image

jakub_l
2518
accept rate: 0%

edited 22 Dec '15, 17:36


Contrary to what you will see in many examples on the web, you do not need to do anything in your WSGI handler to activate your virtualenv.

The correct way to activate your virtualenv is explained by the author of mod_wsgi in his blog: Using Python virtual environments with mod_wsgi

In short, all you need to do is add a python-home parameter to the WSGIDaemonProcess directive in your httpd.conf, like this:

WSGIDaemonProcess django processes=2 threads=12 python-home=/home/username/path/to/virtualenv python-path=/home/username/webapps/appname/projectname

(That should all be on a single line.)

permanent link

answered 22 Dec '15, 20:15

seanf's gravatar image

seanf ♦♦
11.5k21233
accept rate: 37%

edited 22 Dec '15, 20:19

Thanks, Sean. It looks pretty streightforward but although I tried almost every possible path, it still raises the ImportError for django module. I also included path to "site-packages" directory as advised by the blog post. This is the part of my httpd.conf: WSGIDaemonProcess app_name processes=2 threads=12 python-home=/home/username/webapps/app_name/env python-path=/home/username/webapps/app_name/project_name:/home/username/webapps/app_name/env/lib/python3.4/site-packages

(23 Dec '15, 08:40) jakub_l jakub_l's gravatar image

You do not need to include the site-packages directory in the python-path parameter.

What's the exact error that you're seeing?

(23 Dec '15, 13:37) seanf ♦♦ seanf's gravatar image

Well, it turned out to be a "Target WSGI script cannot be loaded as Python module" error. Is that right that python-home targets a root of my virtualenv dir and python-path a dir where manage.py lies? Thanks

(23 Dec '15, 23:26) jakub_l jakub_l's gravatar image

Is that right that python-home targets a root of my virtualenv dir...

Yes.

... and python-path a dir where manage.py lies?

Yes, and you can include any other paths that contain Python modules that aren't in your virtualenv.

If you post the entire traceback that came with that error message, we can probably help you further.

(23 Dec '15, 23:51) seanf ♦♦ seanf's gravatar image

Thank you, Sean! Eventually, this worked for me: WSGIDaemonProcess appname processes=2 threads=12 python-home=/home/username/webapps/appname/env python-path=/home/username/webapps/appname/env/lib/python3.4:/home/username/webapps/appname/path/to/dir/with/managepy. So, your solution was perfectly right... the last part (path to manage.py) was necessary because of my project structure (manage.py is not in the root of my project folder)

(27 Dec '15, 12:50) jakub_l jakub_l's gravatar image
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
×61
×50
×23

question asked: 22 Dec '15, 17:35

question was seen: 2,478 times

last updated: 27 Dec '15, 12:50

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