How to use virtualenvwrapper in Supervisor?
Asked Answered
A

3

26

When I was developing and testing my project, I used to use virtualenvwrapper to manage the environment and run it:

workon myproject
python myproject.py

Of course, once I was in the right virtualenv, I was using the right version of Python, and other corresponding libraries for running my project.

Now, I want to use Supervisord to manage the same project as it is ready for deployment. The question is what is the proper way to tell Supervisord to activate the right virtualenv before executing the script? Do I need to write a separate bash script that does this, and call that script in the command field of Supervisord config file?

Angelineangelique answered 4/3, 2013 at 13:33 Comment(0)
A
57

One way to use your virtualenv from the command line is to use the python executable located inside of your virtualenv.

for me i have my virtual envs in .virtualenvs directory. For example

/home/ubuntu/.virtualenvs/yourenv/bin/python

no need to workon

for a supervisor.conf managing a tornado app i do:

command=/home/ubuntu/.virtualenvs/myapp/bin/python /usr/share/nginx/www/myapp/application.py --port=%(process_num)s
Arst answered 4/3, 2013 at 14:9 Comment(6)
and does this also automatically deal with all the other libraries I pip installed inside that particular virtualenv correctly? Or I need to set those up separately with Supervisor separately?Angelineangelique
related to above, do i need to fiddle with environment setting in the supervisor config?Angelineangelique
@Angelineangelique - by using your virtualenv python this will automatically allow you to access all packages installed in that virtualenv, I have never needed to use environment. I guess using it depends on whether you need to set environmentally variables for your processArst
I found sth wrong. When I do 'source .../activate ; python script.py', there is one process, but when I do '/path/to/env/python script.py', there are two. And in later condition, supervisor can not restart the program correctly.Corniculate
What if you do stuff in bin/postactivate and bin/postdeactivate, like setting environment variables? Doesn't seem like this approach would handle that correctly.Shepherd
He clearly said that you get packages and python that you won't get environmental variables. However that being said especially if there is only few of those you can set them manually. Does it going to work? command=MY_VAR=13 /home/ubuntu/.virtualenvs/myapp/bin/python /usr/share/nginx/www/myapp/application.pyCoca
A
12

Add your virtualenv/bin path to your supervisord.conf's environment:

[program:myproj-uwsgi]
process_name=myproj-uwsgi
command=/home/myuser/.virtualenvs/myproj/bin/uwsgi
    --chdir /home/myuser/projects/myproj
    -w myproj:app
environment=PATH="/home/myuser/.virtualenvs/myproj/bin:%(ENV_PATH)s"
user=myuser
group=myuser
killasgroup=true
startsecs=5
stopwaitsecs=10
Anagoge answered 5/2, 2016 at 16:21 Comment(1)
I haven't tried this yet, but it seems to be the most reasonable way to go. It allows people to run click based clis easily.Ut
C
5

First, run

$ workon myproject
$ dirname `which python`
/home/username/.virtualenvs/myproject/bin

Add the following

environment=PATH="/home/username/.virtualenvs/myproject/bin"

to the related supervisord.conf under [program:blabla] section.

Corvese answered 11/9, 2013 at 16:51 Comment(4)
This didn't work for me. supervisord still uses the regular PATH.Tartuffe
What is the version you're using?Hanrahan
I'm using supervisord 3.1.3Tartuffe
I had similar problem with above solution not working, until I realized that after changing config it's worth to restart supervisord. I do not say it's you problem, I do say future stackoverflow dwellers restart supervisord just in case. :-)Coca

© 2022 - 2024 — McMap. All rights reserved.