Skip to content
Advertisement

Running a python script in virtual environment with node.js pm2

I would like to reference this question because I am certain that someone will flag this as a duplicate.

I am not looking for another reference to supervisord. I’m sure that it is great and all, but the node PM2 has the functionality that I require and is more straightforward to implement and test.

Manual Start

During prototyping, I created a virtual environment called ‘p3env’. At the top of each script, I place a bash directive:

#!./py3env/bin/python

This allows me to execute each script in the directory using this particular python environment without having to activate it. It is very convenient and useful and the python script works well when I start it by hand.

I should be clear about what I mean when I say ‘start it by hand’. My script is called ‘strain_to_db.py’. When I start it by hand, I am on the shell via ssh:

./strain_to_db.py

This gets everything working that I need to have working.

PM2 Start

Moving from relative to absolute paths

To get pm2 working, I started with:

pm2 start ./strain_to_db.py

Specifying the Interpreter

Apparently pm2 ignores the directive at the top of the python script and attempts to execute using the global ‘python’. No problem, I can specify the interpreter:

pm2 start ./strain_to_db.py --interpreter /home/ubuntu/db_if/p3env/bin/python

No dice. Again, maybe try more absolute paths:

pm2 start /home/ubuntu/db_if/strain_to_db.py --interpreter /home/ubuntu/db_if/p3env/bin/python

Running script as command-line option

Now I’m getting frustrated. I try another tactic. I attempt to run the python executable in the command line using:

/home/ubuntu/db_if/p3env/bin/python /home/ubuntu/db_if/strain_to_db.py

This works fine when pm2 isn’t involved. When I try to pass this to pm2 using the ‘command line argument’ style:

pm2 start /home/ubuntu/db_if/p3env/bin/python -- /home/ubuntu/db_if/strain_to_db.py

Frustration

Same error. The error is always ‘can’t import pymysql’, which is only installed on the virtual environment.

I am not sure where else to go with this. I have several scripts that I want to add to the pm2 execution monitor, but I can’t seem to get one of them to start and run correctly.

Advertisement

Answer

After looking around a bit more, the question that I referenced at the top of the email had a clue in one of the answers, but not the answer.

When files end in ‘.py’, pm2 calls ‘python’… no matter what. I believe that there is a configuration file in pm2 that you could modify to change this behavior. I simply removed the ‘.py’ from my script and specified the interpreter:

pm2 start ./strain_to_db --interpreter ./py3env/bin/python

Works perfectly. When I use pm2 to create a startup script, I will use absolute paths. Thanks for anyone who was looking, and I hope this helps someone in the future.

Advertisement