1
votes

I have a python script called main.py that imports RPi.GPIO library using import RPi.GPIO as GPIO When I run the script using python3 main.py I get an error that states RPi.GPIO was not found. If I run main.py using sudo python3 main.py then everything runs fine.

I installed RPi.GPIO using a tar.gz file. I copied it to my /home/pi/work directory and extracted the tarball in the same directory. I then changed to the extracted directory and installed RPi.GPIO using sudo python3 setup.py install.

As I don't normally use linux I don't fully understand the permissions which I am sure is what is causing this issue. I am guessing that since I installed using sudo that the package is only available to the sudo user. The problem is I am starting this program from rc.local file and the main.py script wont run at startup with the RPi.GPIO import statement. If I remove the import statement it starts as expected. Below is the code in the rc.local file that starts the program su -l pi -c '/usr/bin/python3 /home/pi/Work/main.py &' I tried changing su to sudo but that did not work.

Is there a different way I can install RPi.GPIO or change the rc.local script to get this working? Also FYI my pi has no internet connection so I can't use APT-GET to uninstall or install the package.

Also just in case some of you wonder if the package installed properly it has. If I start python with sudo python3 I get >>>. I then type import RPi.GPIO as GPIO I get >>> again. Then I type GPIO.VERSION it displays the correct version I installed. Any help would be appreciated.

UPDATE I did not create the code for the rc.local file and looked a little closer at it. The statement su -l pi -c '/usr/bin/python3 /home/pi/Work/main.py &' I found out changes the user from root to pi and executes the script under the pi user. So I tried changing the statement to su -l sudo -c '/usr/bin/python3 /home/pi/Work/main.py &' thinking that since I can run the main.py by using sudo python3 main.py that changing user from pi to sudo in the rc.local file would execute the file as sudo. It still does not work. I then tried removing the su command from the rc.local command and ran like this /usr/bin/python3 /home/pi/Work/main.py & but this also didn't work. Does anyone have any suggestions on how I can get this to work?

2

2 Answers

0
votes

Try to write bash script with sleep before running python script and put it into rc.local

#!/bin/sh

sleep 5
python python_script.py &

Don't forget to make script executable: chmod 755 yourscript.sh

0
votes

For developing in Python, on Linux or any OS, one would almost always use a virtualenv, one for each python project I want to develop.

A virtualenv is easy to set up, and once activated, you can execute your pip install commands without using sudo. Try setting up a virtualenv and installing GPIO via pip.

Even if you don't set up a virtualenv to help with package management for your Python project, you can still use pip to install GPIO, but you'll face the permission issues you're dealing with now.

But, you're in luck! GPIO is already installed on Raspbian. Open up the Terminal and type python. Once you're in the Python interpreter, type import RPi.GPIO. If you get an error, there is a genuine issue with your installation, but it can be easily overcome by using a virtualenv. It may help to know which is your default python, with python --version.

Pip is conceptually similar to the apt package manager you've probably used with your Raspberry Pi to install other pieces of software. It's a package manager for Python, basically a registry of libraries you can install instantly on the command line.

The command line is your friend on an RPi, especially when it comes to developing original software.

Good luck!