Question

Usinv virtualenv in django image?

Hello!

I’ve used Django on Ubuntu image to create my droplet and it all worked well until I’ve tried to use virtualenv for django_project dependencies.

I’ve created home/django/venvs dir. Within it I’ve created venv that I have installed my app’s dependencies to.

Then I’ve edited upstart file to following:

description "Gunicorn daemon for Django project"

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]

# If the process quits unexpectadly trigger a respawn
respawn

setuid django
setgid django
chdir /home/django

script
    source venvs/myapp/bin/activate
end script

exec gunicorn \
    --name=django_project \
    --pythonpath=django_project \
    --bind=0.0.0.0:9000 \
    --config /etc/gunicorn.d/gunicorn.py \
    django_project.wsgi:application

However Gunicorn fails to start app, which crashes on import of module thats in venv, but not in global python.

Question is what I am doing wrong? How to make default django_project use venv?

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

I am having a similar issue I have a django project(forte) running from a virtualenv(prod)

If I

root@Fortelinea:/# cd /home/django/forte/
root@Fortelinea:/home/django/forte# /home/django/prod/bin/gunicorn forte.wsgi -b 127.0.0.1:9000
[2015-02-04 18:47:02 -0500] [1339] [INFO] Starting gunicorn 19.2.0
[2015-02-04 18:47:02 -0500] [1339] [INFO] Listening at: http://127.0.0.1:9000 (1339)
[2015-02-04 18:47:02 -0500] [1339] [INFO] Using worker: sync
[2015-02-04 18:47:02 -0500] [1342] [INFO] Booting worker with pid: 1342

then everything is groovy and works

now I am trying to use upstart to lauch my virtualenv gunicorn at boot

my ustart script looks like (/etc/init/forte.conf)


start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]

# If the process quits unexpectadly trigger a respawn
respawn

setuid django
setgid django
chdir /home/django/forte

exec /home/django/prod/bin/gunicorn forte.wsgi -b 127.0.0.1:9000

when this runs it the process fails and spawns a bunch of time and then quits

root@Fortelinea:/# start forte
forte start/running, process 1359
root@Fortelinea:/# tail /var/log/syslog
Feb  4 18:52:37 Fortelinea kernel: [ 1760.050336] init: forte main process (1389) terminated with status 3
Feb  4 18:52:37 Fortelinea kernel: [ 1760.050350] init: forte main process ended, respawning
Feb  4 18:52:38 Fortelinea kernel: [ 1760.431913] init: forte main process (1394) terminated with status 3
Feb  4 18:52:38 Fortelinea kernel: [ 1760.431927] init: forte main process ended, respawning
Feb  4 18:52:38 Fortelinea kernel: [ 1760.815041] init: forte main process (1399) terminated with status 3
Feb  4 18:52:38 Fortelinea kernel: [ 1760.815057] init: forte main process ended, respawning
Feb  4 18:52:39 Fortelinea kernel: [ 1761.194726] init: forte main process (1404) terminated with status 3
Feb  4 18:52:39 Fortelinea kernel: [ 1761.194741] init: forte main process ended, respawning
Feb  4 18:52:39 Fortelinea kernel: [ 1761.577727] init: forte main process (1409) terminated with status 3
Feb  4 18:52:39 Fortelinea kernel: [ 1761.577744] init: forte respawning too fast, stopped

any thoughts or insights would be appreciated

Thank you for your attention Andrew.

I think I’ve got it fixed, but there’s one gotcha:

For some reason latest gunicorn that worked for me was 17.5 (the one that came preinstaled with image).

I’ve got app to work, but there are still quirks, which I will solve myself now (fingers crossed)

The script block executes on its own. So the virtualenv will not be available to gunicorn when started like that. Check gunicorn’s docs on using it with a virtualenv:

To serve an app from a Virtualenv it is generally easiest to just install Gunicorn directly into the Virtualenv. This will create a set of Gunicorn scripts for that Virtualenv which can be used to run applications normally.

So install it directly in the virtualenv:

source ~/venvs/myapp/bin/activate
pip install -I gunicorn

Then in your Upstart script, you’d need to use gunicorn from the virtualenv. It would be something like:

description "Gunicorn daemon for Django project"

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]

# If the process quits unexpectadly trigger a respawn
respawn

setuid django
setgid django
chdir /home/django

exec venvs/myapp/bin/gunicorn \
    --name=django_project \
    --pythonpath=django_project \
    --bind=0.0.0.0:9000 \
    --config /etc/gunicorn.d/gunicorn.py \
    django_project.wsgi:application