回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>有了几十个以前的答案,我很惊讶我什么都找不到。。。你知道吗</p>
<p>我将<code>paho</code>mqtt库用于一个非常简单的python程序来报告一些数据(在Raspberry Pi上运行)。从python程序中导入_程序.py)是:</p>
<pre><code>import paho.mqtt.client as mqtt
</code></pre>
<p>如果我使用<code>python my_program.py</code>从命令行运行程序,它运行时不会出错。但是,我正在尝试将其设置为系统服务来管理其执行。我用类似的python程序做了十几次,设置了各种bash脚本和服务文件。他们都工作除了这个。我提到这一点是因为我不认为它与bash或服务本身有关。为了完整起见,这里是设置。你知道吗</p>
<p>从运行-</p>
<pre><code>## Service (my_program.service):
ExecStart=/home/pi/my_program.sh
## and bash (my_program.sh)
python /home/pi/my/directory/my_program.py
</code></pre>
<p>当我开始服务时,我得到:</p>
<pre><code>pi@ArmstrongSE:/etc/systemd/system $ sudo systemctl status my_program
* my_program.service
Loaded: loaded (/etc/systemd/system/my_program.service; static; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2019-11-23 13:59:58 PST; 22s ago
Process: 31100 ExecStart=/home/pi/my_program.sh (code=exited, status=1/FAILURE)
Main PID: 31100 (code=exited, status=1/FAILURE)
Nov 23 13:59:54 ArmstrongSE systemd[1]: Started my_program.service.
Nov 23 13:59:54 ArmstrongSE my_program.sh[31100]: Starting MQTT Transmitter
Nov 23 13:59:55 ArmstrongSE my_program.sh[31100]: /home/pi/data/solar/20191123135605.json
Nov 23 13:59:58 ArmstrongSE my_program.sh[31100]: Traceback (most recent call last):
Nov 23 13:59:58 ArmstrongSE my_program.sh[31100]: File "/home/pi/my/directory/my_program.py", line 25, in <module>
Nov 23 13:59:58 ArmstrongSE my_program.sh[31100]: import paho.mqtt.client as mqtt
Nov 23 13:59:58 ArmstrongSE my_program.sh[31100]: ImportError: No module named paho.mqtt.client
</code></pre>
<p>要确认路径分配是否存在--</p>
<p>从python解释器中我得到:</p>
<pre><code>>>> import paho.mqtt.client as mqtt
>>> print(mqtt.__file__)
/home/pi/.local/lib/python2.7/site-packages/paho/mqtt/client.pyc
</code></pre>
<p><code>sys.path</code>报告:</p>
<pre><code>>>> import sys
>>> print(sys.path)
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-arm-linux-gnueabihf',
'/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload',
'/home/pi/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/gtk-2.0']
</code></pre>
<p>我被困住了。。。你有没有想过为什么这个不能加载?你知道吗</p>
<p><strong>更新/澄清:</strong></p>
<p>我可以从任何地方(任何目录)运行bash脚本,它可以正常工作。所以,它一定是<code>systemd</code>设置(?)中的某个东西。你知道吗</p>