Running python script in virtual environment using node.js pm2

I would like to refer to this question because I am sure that someone will mark this as a duplicate.

I am not looking for another link to supervisord . I am sure that this is great and that’s all, but node PM2 has the functions that I need and is simpler to implement and test them.

Manual start

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

#!./py3env/bin/python 

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

It should be clear to me what I mean when I say "start it by hand." My script is called 'strain_to_db.py'. When I run it manually, I am in the shell via ssh:

 ./strain_to_db.py 

This gets everything I need to work.

PM2 Start

Transition from relative to absolute paths

To get pm2 to work, I started with:

 pm2 start ./strain_to_db.py 

Interpreter task

Obviously, pm2 ignores the directive at the top of the python script and tries 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, perhaps try more absolute paths:

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

Running a script as a command line option

Now I'm upset. I try different tactics. I am trying to run python executable on the command line using:

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

This works fine when pm2 is not 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 

Disappointment

The same mistakes. The error is always "cannot import pymysql", which is installed only in a virtual environment.

I'm not sure where else to go with this. I have several scripts that I want to add to the pm2 runtime monitor, but I cannot get them to start and run correctly.

+6
source share
2 answers

Having examined a little more, the question to which I referred at the top of the letter had the key to one of the answers, but not the answer.

When the files end with ".py", pm2 calls "python" ... no matter what. I believe that in pm2 there is a configuration file that you can modify to change this behavior. I just removed the ".py" from my script and pointed out the interpreter:

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

Works great. When I use pm2 to create a startup script, I will use absolute paths. Thanks to everyone who watched, and I hope this helps someone in the future.

+15
source

I created the echosystem.config.json file

 { "apps": [{ "name": "app_name", "script": "/the/app/path/my_app.py", "args": ["-c", "my_config.prod.json"], "instances": "1", "wait_ready": true, "autorestart": false, "max_restarts": 5, "interpreter" : "/path/to/venv/bin/python", }] } 

Start the pm2 service:

 $ pm2 start echosystem.config.json $ pm2 -v 3.2.8 
0
source

Source: https://habr.com/ru/post/1245333/


All Articles