主管 - 运行 python 脚本 PYTHONPATH 问题

发布于 2024-12-12 07:14:47 字数 947 浏览 0 评论 0原文

我正在使用主管运行 python 脚本:

[program:twitter_track]
autorestart = true
numprocs = 1
autostart = false
redirect_stderr = True
stopwaitsecs = 1
startsecs = 1
priority = 99
command = python /home/ubuntu/services/twitter.py track
startretries = 3
stdout_logfile = /home/ubuntu/logs/twitter_track.log

但进程无法启动。以下是错误日志的内容:

Traceback (most recent call last):
  File "/home/ubuntu/services/twitter.py", line 6, in <module>
    from mymodule.twitter.stream import TwitterStream
ImportError: No module named mymodule.twitter.stream
Traceback (most recent call last):
  File "/home/ubuntu/services/twitter.py", line 6, in <module>

似乎获取了 mymodule,但是如果我自己运行 twitter.py,一切正常,只有当我通过主管运行它时才会抛出此错误。

我将 mymodule 添加到我的 ~/.profile 文件中的 PYTHONPATH 中,如下所示:

export PYTHONPATH=$PYTHONPATH:/home/ubuntu/lib

是否有任何原因导致脚本在通过终端运行时有效,但在通过主管运行时无效?任何帮助将不胜感激。

I am using supervisor to run a python script:

[program:twitter_track]
autorestart = true
numprocs = 1
autostart = false
redirect_stderr = True
stopwaitsecs = 1
startsecs = 1
priority = 99
command = python /home/ubuntu/services/twitter.py track
startretries = 3
stdout_logfile = /home/ubuntu/logs/twitter_track.log

But the process fails to start. Here is what the error log says:

Traceback (most recent call last):
  File "/home/ubuntu/services/twitter.py", line 6, in <module>
    from mymodule.twitter.stream import TwitterStream
ImportError: No module named mymodule.twitter.stream
Traceback (most recent call last):
  File "/home/ubuntu/services/twitter.py", line 6, in <module>

It seems that obtain mymodule, but if I run twitter.py on it's own, everything works fine, it only throws this error when I run it through supervisor.

I added mymodule to the PYTHONPATH in my ~/.profile file like so:

export PYTHONPATH=$PYTHONPATH:/home/ubuntu/lib

Is there any reason why the script would work when run through terminal but not when run through supervisor? Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吹泡泡o 2024-12-19 07:14:47

PYTHONPATH 定义添加到supervisord 配置文件中的environment 指令中。它应该位于您的 [program:twitter_track] 部分下,如下所示:

environment=PYTHONPATH=/home/ubuntu/lib/

这将确保您的 python 进程在supervisord 启动时看到正确的 PYTHONPATH

Add the PYTHONPATH definition to the environment directive in the supervisord configuration file. It should go under your [program:twitter_track] section, like so:

environment=PYTHONPATH=/home/ubuntu/lib/

This will ensure that that your python process sees the proper PYTHONPATH when supervisord starts it.

寂寞笑我太脆弱 2024-12-19 07:14:47

将 PYTHONPATH 定义添加到环境中:

[program:twitter_track]
command = python /home/ubuntu/services/twitter.py track
environment=PYTHONPATH=/home/ubuntu/lib

Add the PYTHONPATH definition to the environment:

[program:twitter_track]
command = python /home/ubuntu/services/twitter.py track
environment=PYTHONPATH=/home/ubuntu/lib
小伙你站住 2024-12-19 07:14:47

@Darren Griffith 给出正确的解决方案。但如果我们送走文档
http://supervisord.org/configuration.html#program-x-section-设置 ->环境
我们哭了,它说

格式为 KEY="val",KEY2="val2" 的键/值对列表

因此您需要使用引号 ""

[program:twitter_track]
command = python /home/ubuntu/services/twitter.py track
environment=PYTHONPATH="/home/ubuntu/lib"

@Darren Griffith give right solution. But if we see off docs
http://supervisord.org/configuration.html#program-x-section-settings -> environment
We wee, that it says

A list of key/value pairs in the form KEY="val",KEY2="val2"

So you need use a quotes ""

[program:twitter_track]
command = python /home/ubuntu/services/twitter.py track
environment=PYTHONPATH="/home/ubuntu/lib"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文