使用Shell脚本时Python 3导入的问题

2024-05-15 15:52:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下情况:

  • 通过pip安装的Python3软件包(meross_iot),位于 ~/.local/lib/python3.7/site-packages
  • 从这个包导入的Python脚本(meross_electronic.py):from meross_iot.controller.mixins.electricity import ElectricityMixin
  • 一个shell脚本(launcher.sh),用于作为包装器,以便在启动时运行.py脚本:
    #!/bin/sh
    # launcher.sh
    # navigate to home directory, then to this directory, then execute python script, then back home
    
    cd /
    cd home/pi/Documents
    sudo python meross_electricity.py
    cd /
    

如果我只是执行.py文件,则一切正常,导入完成,等等。如果我尝试运行.sh脚本,则会出现以下错误:

pi@home:~/Documents $ ./launcher.sh
Traceback (most recent call last):
  File "meross_electricity.py", line 4, in <module>
    from meross_iot.controller.mixins.electricity import ElectricityMixin
ModuleNotFoundError: No module named 'meross_iot'

有人能帮我解决这个问题吗? 谢谢


Tags: frompyimport脚本homeshcdmixins