python 脚本从 rc.local 启动时导入 opentype 时出现问题

Issue with the import of opentype when python script is started from rc.local

我想做的是在 Raspbian 启动时执行 python 脚本。我这样做的方法是:

#!/bin/sh -e
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi
/usr/bin/python /home/pi/Desktop/MyProj/sample/main.py > /home/pi/Desktop/projStartup.log 2>&1 &
exit 0

它的编写方式基本上意味着我将它作为一个分支进程启动('&' 最后)并且每个日志都应该进入 projStartup.log 文件.

当我打开终端并输入:

pi@raspberrypi:~ $ /etc/rc.local

一切都按预期工作,但是当我尝试重新启动我的 Raspbian 时,出现以下错误(错误堆栈的顶部):

File "/home/pi/Desktop/MyProj/sample/firebaseManager.py", line 1, in <module>
    from pyrebase import pyrebase
  File "/usr/local/lib/python2.7/dist-packages/pyrebase/__init__.py", line 1, in <module>
    from .pyrebase import initialize_app
  File "/usr/local/lib/python2.7/dist-packages/pyrebase/pyrebase.py", line 17, in <module>
    from oauth2client.service_account import ServiceAccountCredentials
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/service_account.py", line 26, in <module>
    from oauth2client import crypt
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/crypt.py", line 23, in <module>
    from oauth2client import _pure_python_crypt
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/_pure_python_crypt.py", line 24, in <module>
    from pyasn1_modules.rfc2459 import Certificate
  File "/usr/local/lib/python2.7/dist-packages/pyasn1_modules/rfc2459.py", line 20, in <module>
    from pyasn1.type import opentype
ImportError: cannot import name opentype

如您所见,opentype 的导入存在问题。我的脚本正在使用的其他模块也有类似的问题,解决方案是使用以下命令全局安装它们:

sudo pip install ...

所以,现在我想知道需要在全球范围内更新哪些内容才能避免这种开放类型问题。

如@thebjorn 所建议

解决方案:我的解决方案是明确设置 pi 用户(我在登录时使用的用户)。

sudo -H -u pi /usr/bin/python /home/pi/Desktop/MyProj/sample/main.py > /home/pi/Desktop/projStartup.log 2>&1 &