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

Viewed 17693

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.

3 Answers

A bit late to this question but for anyone coming here with a fresh pair of eyes, I've found that if you activate the virtual environment eg. source venv/Scripts/activate then start your script via pm2 eg. pm2 start main.py --name migration, it will automatically use the environment you've activated.

Related