Search code examples
pythonraspbianpython-moduleopentype

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


What I am trying to do is to execute a python script on a Raspbian startup. The way I do it is by putting this:

#!/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

The way it is written basically means that I am starting it as a forked process ('&' at the end) and each log should go into the projStartup.log file.

When I open the terminal and type:

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

everything works as expected but when I try to restart my Raspbian I get the following error (top of the error stack):

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

As you can see, there's an issue with the import of opentype. I had similar issues with other modules that are being used by my script and the solution was to install them globally by using:

sudo pip install ...

So, now I am wondering what needs to be updated on a global level in order to be able to avoid this opentype issue.


Solution

  • as suggested by @thebjorn

    SOLUTION: The solution for me was to explicitly set the pi user (the one that I use when logged in).

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