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

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.


This Medium article solved this problem for me.

https://medium.com/@gokhang1327/deploying-flask-app-with-pm2-on-ubuntu-server-18-04-992dfd808079

Command for running a python script in virtual enviroment:

pm2 start app.py --name flask-app --interpreter=python3

--name it´s optional, that´s the name of process displayed in pm2 status

Result:

enter image description here

"new" is the name of my virtualenv environment.


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.