简体   繁体   English

使用 node.js pm2 在虚拟环境中运行 python 脚本

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

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 .我不是在寻找对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.我确信它很棒,但是节点 PM2 具有我需要的功能,并且更易于实现和测试。

Manual Start手动启动

During prototyping, I created a virtual environment called 'p3env'.在原型设计期间,我创建了一个名为“p3env”的虚拟环境。 At the top of each script, I place a bash directive:在每个脚本的顶部,我放置了一个 bash 指令:

#!./py3env/bin/python

This allows me to execute each script in the directory using this particular python environment without having to activate it.这允许我使用这个特定的 python 环境执行目录中的每个脚本,而无需激活它。 It is very convenient and useful and the python script works well when I start it by hand.它非常方便和有用,当我手动启动它时,python 脚本运行良好。

I should be clear about what I mean when I say 'start it by hand'.当我说“手动启动”时,我应该清楚我的意思。 My script is called 'strain_to_db.py'.我的脚本称为“strain_to_db.py”。 When I start it by hand, I am on the shell via ssh:当我手动启动它时,我通过 ssh 在 shell 上:

./strain_to_db.py

This gets everything working that I need to have working.这使我需要工作的一切正常工作。

PM2 Start PM2 启动

Moving from relative to absolute paths从相对路径到绝对路径

To get pm2 working, I started with:为了让 pm2 工作,我开始:

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'.显然 pm2 忽略了 python 脚本顶部的指令并尝试使用全局“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:我尝试使用以下命令在命令行中运行 python 可执行文件:

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

This works fine when pm2 isn't involved.当不涉及 pm2 时,这可以正常工作。 When I try to pass this to pm2 using the 'command line argument' style:当我尝试使用“命令行参数”样式将其传递给 pm2 时:

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.错误总是'can't import pymysql',只安装在虚拟环境中。

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.我有几个要添加到 pm2 执行监视器的脚本,但我似乎无法让其中一个脚本正确启动和运行。

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.当文件以“.py”结尾时,pm2 调用“python”......无论如何。 I believe that there is a configuration file in pm2 that you could modify to change this behavior.我相信 pm2 中有一个配置文件,您可以修改它来改变这种行为。 I simply removed the '.py' from my script and specified the interpreter:我只是从脚本中删除了“.py”并指定了解释器:

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.当我使用 pm2 创建启动脚本时,我将使用绝对路径。 Thanks for anyone who was looking, and I hope this helps someone in the future.感谢任何正在寻找的人,我希望这对未来的人有所帮助。

This Medium article solved this problem for me.这篇 Medium 文章为我解决了这个问题。

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

Command for running a python script in virtual enviroment:在虚拟环境中运行 python 脚本的命令:

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

--name it´s optional, that´s the name of process displayed in pm2 status --name 可选,即在 pm2 状态中显示的进程名称

Result:结果:

在此处输入图像描述

"new" is the name of my virtualenv environment. “new”是我的 virtualenv 环境的名称。

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. source venv/Scripts/activate然后通过 pm2 启动您的脚本,例如。 pm2 start main.py --name migration , it will automatically use the environment you've activated. pm2 start main.py --name migration ,它会自动使用你激活的环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM