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

15,293

Solution 1

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.

Solution 2

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.

Share:
15,293
slightlynybbled
Author by

slightlynybbled

Electrical Engineer with fingers in Mechanical and Software areas. I run a blog called for(embed) which focuses primarily on projects with microcontrollers, hardware and software. I am proficient in embedded C and assembly with the Microchip 8 and 16-bit lines and on the TI MSP430 series. I have used C++ on the AVR and the MSP430 series. In recent years, I have been happy to learn how to create higher-level applications in Python along with some full-stack development utilizing Flask. Check out my github account for some work samples.

Updated on July 02, 2022

Comments

  • slightlynybbled
    slightlynybbled almost 2 years

    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.

  • Dat TT
    Dat TT over 6 years
    Thanks! This will be very useful for virtualenv.
  • Unitech
    Unitech over 6 years
    PM2 maintainer here, this bug has been fixed right? I just tried again and the interpreter option is taken into account as expected regardless to the file extension
  • bosch
    bosch over 6 years
    Not working for me. It says [PM2][ERROR] script not found : /data/python/app/main where main.py would be my executable. Tried also with relative, absolute paths, same folder, manual start but no luck. I use the pm2 version 2.8.0.
  • AnandShiva
    AnandShiva over 3 years
    Any advice on how we can use this when running the flask app? Flask app doesn't start with a single file with a .py extension. And specifying an interpreter for a flask is also a little tricky.