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.
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.
To get pm2 working, I started with:
pm2 start ./strain_to_db.py
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
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
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.
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.
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:
"new" is the name of my virtualenv environment.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.