Hanpan
Hanpan

Reputation: 10261

Supervisor - Running a python script PYTHONPATH issue

I am using supervisor to run a python script:

[program:twitter_track]
autorestart = true
numprocs = 1
autostart = false
redirect_stderr = True
stopwaitsecs = 1
startsecs = 1
priority = 99
command = python /home/ubuntu/services/twitter.py track
startretries = 3
stdout_logfile = /home/ubuntu/logs/twitter_track.log

But the process fails to start. Here is what the error log says:

Traceback (most recent call last):
  File "/home/ubuntu/services/twitter.py", line 6, in <module>
    from mymodule.twitter.stream import TwitterStream
ImportError: No module named mymodule.twitter.stream
Traceback (most recent call last):
  File "/home/ubuntu/services/twitter.py", line 6, in <module>

It seems that obtain mymodule, but if I run twitter.py on it's own, everything works fine, it only throws this error when I run it through supervisor.

I added mymodule to the PYTHONPATH in my ~/.profile file like so:

export PYTHONPATH=$PYTHONPATH:/home/ubuntu/lib

Is there any reason why the script would work when run through terminal but not when run through supervisor? Any help would be appreciated.

Upvotes: 15

Views: 16046

Answers (3)

Galoperidol
Galoperidol

Reputation: 63

@Darren Griffith give right solution. But if we see off docs http://supervisord.org/configuration.html#program-x-section-settings -> environment We wee, that it says

A list of key/value pairs in the form KEY="val",KEY2="val2"

So you need use a quotes ""

[program:twitter_track]
command = python /home/ubuntu/services/twitter.py track
environment=PYTHONPATH="/home/ubuntu/lib"

Upvotes: 0

Eds_k
Eds_k

Reputation: 1144

Add the PYTHONPATH definition to the environment:

[program:twitter_track]
command = python /home/ubuntu/services/twitter.py track
environment=PYTHONPATH=/home/ubuntu/lib

Upvotes: 2

Darren Griffith
Darren Griffith

Reputation: 531

Add the PYTHONPATH definition to the environment directive in the supervisord configuration file. It should go under your [program:twitter_track] section, like so:

environment=PYTHONPATH=/home/ubuntu/lib/

This will ensure that that your python process sees the proper PYTHONPATH when supervisord starts it.

Upvotes: 35

Related Questions