简体   繁体   English

Python Systemd 服务的 ImportError

[英]Python ImportError for systemd service

I am trying to start a python script on a raspby via a systemd service, but it cannot find any of the modules installed via pip3 and gives the error:我正在尝试通过 systemd 服务在 raspby 上启动 python 脚本,但它找不到通过 pip3 安装的任何模块并给出错误:

raspberrypi python3[1017]: ModuleNotFoundError: No module named 'paho'

Running the same script via SSH terminal works fine.通过 SSH 终端运行相同的脚本工作正常。 From my research, it could relate to the PYTHONPATH, though I have been unable to find it in.bashrc根据我的研究,它可能与 PYTHONPATH 有关,尽管我无法在.bashrc 中找到它

The modules that cannot be found are installed here:找不到的模块安装在这里:

./.local/lib/python3.7/site-packages (1.5.0)

This is the service file in /etc/systemd/user/mytest.service which starts the script unsuccessfully:这是 /etc/systemd/user/mytest.service 中的服务文件,它启动脚本失败:

[Unit]
Description=TestScript Service
After=network-online.target

[Service]
Type=idle
ExecStart=/usr/bin/python3 /home/pi/MyProject/my_script.py > /home/pi/my_script.log 2>&1

[Install]
WantedBy=network-online.target

How can I let the service know, where the modules are located?我怎样才能让服务知道模块的位置?

Kind regards亲切的问候

Here's is a quick fix to the problem:这是该问题的快速解决方法:

By specifying a User in the.service file under [Service], the python script will find all installed libraries.通过在 [Service] 下的.service 文件中指定用户,python 脚本将找到所有已安装的库。

[Service]
User=pi

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

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