使用 node.js pm2 在虚拟环境中运行 python 脚本 [英] Running a python script in virtual environment with node.js pm2

查看:88
本文介绍了使用 node.js pm2 在虚拟环境中运行 python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想参考这个问题 因为我确信有人会将此标记为重复.

I would like to reference this question because I am certain that someone will flag this as a duplicate.

我不是在寻找对 supervisord 的其他引用.我确信它很棒,但节点 PM2 具有我需要的功能,并且更易于实现和测试.

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.

在原型设计期间,我创建了一个名为p3env"的虚拟环境.在每个脚本的顶部,我放置了一个 bash 指令:

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

#!./py3env/bin/python

这允许我使用这个特定的 python 环境执行目录中的每个脚本,而无需激活它.它非常方便和有用,并且当我手动启动它时,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.

当我说手动启动"时,我应该清楚我的意思.我的脚本称为strain_to_db.py".当我手动启动它时,我通过 ssh 在 shell 上:

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 工作,我开始于:

To get pm2 working, I started with:

pm2 start ./strain_to_db.py

指定解释器

显然 pm2 忽略了 python 脚本顶部的指令,并尝试使用全局python"执行.没问题,我可以指定解释器:

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

作为命令行选项运行脚本

现在我很沮丧.我尝试另一种策略.我尝试使用以下命令在命令行中运行 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

当不涉及 pm2 时,这可以正常工作.当我尝试使用命令行参数"样式将其传递给 pm2 时:

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

沮丧

同样的错误.一直报错'can't import pymysql',只能安装在虚拟环境中.

Frustration

Same error. The error is always 'can't import pymysql', which is only installed on the virtual environment.

我不知道还有什么地方可以解决这个问题.我有几个脚本想要添加到 pm2 执行监视器,但我似乎无法让其中之一正确启动和运行.

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.

当文件以 '.py' 结尾时,pm2 调用 'python'... 无论如何.我相信 pm2 中有一个配置文件,您可以修改它以更改此行为.我只是从我的脚本中删除了.py"并指定了解释器:

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

完美运行.当我使用 pm2 创建启动脚本时,我会使用绝对路径.感谢所有正在寻找的人,我希望这对未来的人有所帮助.

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.

这篇关于使用 node.js pm2 在虚拟环境中运行 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆