WebFaction
Community site: login faq

Trying to write a bash script to install django in a virtualenv (i know it's a classic webfaction question, but please keep reading!)

Setting up the usual way.. create the app in webfactions' control panel, then create a venv (pyvenv-3.5) and point the wsgi.py to httpd.conf. here's an old blog i did with these steps: https://www.josephmohan.xyz/blog/webfaction-mezzanine-and-pyvenv/

Now.. check this from my terminal (edited for conciseness):

pyvenv-3.5 test
cd test
source bin/activate
(test) pip install mezzanine (actually installing mezzanine this time, which uses **django==1.10.7**)
pip freeze = Django==1.10.7

now if i cd into the webapps directory:

cd 
cd webapps/test/my_cms
pip freeze = Django==1.10.5 (It's now seeing outside the environment!?)

So.... /pyvenvs/venv returns django==1.10.7 whereas calling it in /webapps/test/my_cms returns django==1.10.5..

How can this be?

Here's my httpd.conf:

ServerRoot "/home/ipra/webapps/test/apache2"

LoadModule authz_core_module modules/mod_authz_core.so
LoadModule dir_module        modules/mod_dir.so
LoadModule env_module        modules/mod_env.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module       modules/mod_mime.so
LoadModule rewrite_module    modules/mod_rewrite.so
LoadModule setenvif_module   modules/mod_setenvif.so
LoadModule wsgi_module       modules/mod_wsgi.so
LoadModule unixd_module      modules/mod_unixd.so

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /home/ipra/logs/user/access_test.log combined
ErrorLog /home/ipra/logs/user/error_test.log

Listen xxxxx
KeepAlive Off
SetEnvIf X-Forwarded-SSL on HTTPS=1
ServerLimit 1
StartServers 1
MaxRequestWorkers 5
MinSpareThreads 1
MaxSpareThreads 3
ThreadsPerChild 5
HostnameLookups Off
WSGIRestrictStdout Off

WSGIDaemonProcess test processes=2 threads=12 python-home=/home/ipra/pyvenvs/test
WSGIProcessGroup test
WSGIRestrictEmbedded On
WSGILazyInitialization On
WSGIScriptAlias / /home/ipra/webapps/test/myproject/myproject/wsgi.py

<VirtualHost *:xxxxx>
 WSGIScriptAlias / /home/ipra/webapps/test/my_cms/my_cms/wsgi.py
 ServerName mysite.com
 ServerAlias www.mysite.com
 ServerAdmin web@mysite.com
 ErrorLog "/home/ipra/logs/user/error_test.log"
</VirtualHost>

And here's the wsgi.py:

import os, sys, site 
from django.core.wsgi import get_wsgi_application
site.addsitedir('/home/ipra/pyvenvs/test/lib/python3.5/site-packages')
os.environ['DJANGO_SETTINGS_MODULE'] = 'my_cms.settings'
activate_this = os.path.expanduser("~/pyvenvs/test/bin/activate_this.py")
with open(activate_this) as f:
    code = compile(f.read(), activate_this, 'exec')
    exec(code, dict(__file__=activate_this))
project = '/home/ipra/webapps/test/my_cms/'
workspace = os.path.dirname(project)
sys.path.append(workspace)
sys.path = ['/home/ipra/webapps/test/my_cms', '/home/ipra/webapps/test/my_cms/my_cms', 
'/home/ipra/webapps/test'] + sys.path
application = get_wsgi_application()

activate_this.py:

import sys
import os
__file__ = os.path.realpath(__file__)

old_os_path = os.environ['PATH']
os.environ['PATH'] = os.path.dirname(os.path.abspath(__file__)) + os.pathsep + old_os_path
base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if sys.platform == 'win32':
    site_packages = os.path.join(base, 'Lib', 'site-packages')
else:
    site_packages = os.path.join(base, 'lib', 'python%s' % sys.version[:3], 'site-packages')
prev_sys_path = list(sys.path)
import site
site.addsitedir(site_packages)
sys.real_prefix = sys.prefix
sys.prefix = base
# Move the added items to the front of the path:
new_sys_path = []
for item in list(sys.path):
    if item not in prev_sys_path:
        new_sys_path.append(item)
        sys.path.remove(item)
sys.path[:0] = new_sys_path

asked 08 Apr, 12:36

joeskru's gravatar image

joeskru
133
accept rate: 0%

edited 11 Apr, 14:13

Can you post the WSGI directives from your Apache config, and any sys.path modifications from your WSGI handler?

(08 Apr, 17:09) seanf ♦♦ seanf's gravatar image

@seanf, hi please see edited question. I still have not found a solution to this, i've started to look into the "activate_this.py" (used by the wsgi.py). I'm using this because it fixed a problem i had historically but maybe it is not necessary now, trouble is i don't really know what it does! i got the code from here: http://devmartin.com/blog/2015/02/how-to-deploy-a-python3-wsgi-application-with-apache2-and-debian/

(11 Apr, 14:11) joeskru joeskru's gravatar image

I think pyvenv must work differently from virtualenv somehow, as it's definitely not respecting your blank sitecustomize.py.

In this case, the simplest solution will be to delete your /home/ipra/webapps/test/lib directory - it doesn't contain any libraries you want or need.

Regarding your Apache and WSGI config, you don't need all of that addsitedir, sys.path, and activate_this stuff.

The python-home parameter on your WSGIDaemonProcess directive is what points the app at your virtualenv.

If your app needs additional search paths for Python, then you can add them to the python-path parameter, eg:

WSGIDaemonProcess test processes=2 threads=12 python-home=/home/ipra/pyvenvs/test python-path=/home/ipra/webapps/test/my_cms:/home/ipra/webapps/test/my_cms/my_cms:/home/ipra/webapps/test
permanent link

answered 12 Apr, 00:13

seanf's gravatar image

seanf ♦♦
11.4k21132
accept rate: 37%

Removed all the crud from wsgi.py, added the path to the httpd.conf and all works great. Still not working correctly with sitecustomize.py but like you said removing the useless /lib directory does the trick. Thanks for your help, cleared up a lot of stuff!

(13 Apr, 09:13) joeskru joeskru'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:

×875
×49
×23
×6

question asked: 08 Apr, 12:36

question was seen: 194 times

last updated: 13 Apr, 09:13

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