Search code examples
pythonlibrariesboot

python library loads and works properly from command line, but 'ImportError: No module' if I try to run at boot


I’m trying to run a couple of python programs at boot time (in connection with some halloween costumes I’ve been making with my kids; I don’t want somebody to have to log in remotely to start the software in their costumes if they have to reboot for some reason).

The software works perfectly if run from the command line, whether launched from its own directory or any other. However if I try to run at launch by putting the script in rc.local, nothing happens and I log the following error:

Traceback (most recent call last):
  File "/home/pi/sound.py", line 4, in <module>
    from pad4pi import rpi_gpio
ImportError: No module named pad4pi

Additional information: Some additional color: which python returns '/usr/bin/python’ and I have fully specified in rc.local that this is the python to use (/usr/bin/python /home/pi/sound.py &)

I installed with pip install pad4pi, and checking a moment ago it confirms that pad4pi in ./.local/lib/python2.7/site-packages (1.1.5)

Why would the pad4pi library (which is needed in both projects I’m working on) be available from the command line but not at launch? And more importantly, what can I do about it?

[Note, solution below. I will accept the answer when stackOverflow lets me.]


Solution

  • The library was not appearing in $Pythonpath at boot time.

    I added export PYTHONPATH=$PYTHONPATH:/home/pi/.local/lib/python2.7/site-packages to my rc.local file, before it ran my python code, and all was well.