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?
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
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!
Related
I used: "sudo apt-get install idle3" and "sudo apt install idle" in my attempts to install Python IDLE from the terminal. Both worked fine, with a successful installation message afterwards. However, when I type $ idle or $ idle3, nothing happens. When I tried to open it from the "Applications" menu, it also would not open. (Note: I can set up a python file and run it without trouble, so I am sure that both Python2 and 3 are properly installed.) Any suggestions on how to make IDLE run correctly, or maybe to reinstall it a different way so that it actually opens?
I've got a Python script that I need to run upon start up and problem
is that it throws an error saying "no module named xyz". I'm using external library which I installed using pip3. The script works just fine on its own
but I get aforementioned error when I want to run it right after boot.
What should I do ? I tried to delay importing the library with time.sleep(10) in case third-party libraries need few more seconds to load up after boot, but that didn't have desired effect.
I run the script on Raspberry Pi with Debian-based os called Raspbian. I configured execution upon start up by adding this sudo python3 script.py into /etc/profile file.
I don't see how pip install without sudo could have worked.
What I see: scripts run on startup from cron or /etc/profile are run under root, not under pi user. Thus, they don't have the same $PATH, $PYTHONPATH and other environment variable values that you have in your user's shell.
As pip install managed to run without sudo, I suspect that you installed your module into a user-specific directory, which is not a part of root's Python environment.
Replacing the /etc/profile line with sudo -u pi python3 script.py may help.
Whatever it is, it's the difference that is already in the Python environment. Waiting for 10s "for whatever to come up" will not help it.
1 - Download anaconda: https://www.anaconda.com/download/
2 - Create an environment in conda: https://conda.io/docs/user-guide/tasks/manage-environments.html
3 - Activate that environment
4 - conda install or pip3 install your package
It should just work.
I'm trying to run some python code with the sudo command, but every time I do it, it gives me an Import error. However, if I run, say, import numpy in terminal it gives me no errors. Also, if I build a code with several Imports and then run it without the sudo command, it gives me no errors and the code runs flawlessly. I already added Defaults env_keep += "PYTHONPATH" to the sudoers folder, so that's not the problem. I installed Anaconda3, so maybe that's useful information?
I'm running GNOME Ubuntu 16.04.1 LTS. And kernel version 4.4.0-59-generic.
I'm sorry, I'm very new at this, but I'm learning.
I ran which python and then I ran sudo which python and they gave me different directories.
sudo which python gave me usr/bin/python which python gave me home/user/anaconda3/bin/python
I tried running sudo ./anaconda3/envs/ml/bin/python doc.py but now it says that it can't find the file.
I'm running it with sudo because I need the permission for docker to work.
EDIT: trying sudo -E instead of sudo yields the same error.
The problem you have is that sudo does not follow the usual PATH order when looking at an executable: it searches the system directories first. This is written in the man sudo:
SECURITY NOTES
sudo tries to be safe when executing external commands.
To prevent command spoofing, sudo checks "." and "" (both denoting current directory) last when searching for a command in the
user's PATH (if one or both are in the PATH). Note, however, that the actual PATH environment variable is not modified and is passed
unchanged to the program that sudo executes.
So, to fix this you have to make sure that the command you give to sudo cannot match a system executable, i.e. specify the absolute path:
sudo /home/user/anaconda3/bin/python
A general command that should work is:
sudo "$(which python)"
This is because which python is executed before sudo, and its output is passed as an argument to sudo. However sudo by default does not perform any "shell-like" setup, and may restrict the environment, so you may consider using the -E or -i flags to make sudo pass the environment untouched and do the proper shell setup.
I'm trying to run a python script using scapy in Ubuntu 14.04. I downloaded python3 with
sudo apt-get install python3
and I'm running the file I have with
sudo python3 <my filename>.py
As for importing scapy into my python file, I've tried
from scapy.all import *
and
import scapy.all
and other variations I've found while browsing the internet. However, none of them work and I keep getting the "No module named 'scapy'" error.
My script worked when I ran it in python2 in the same environment using scapy, but I've made changes specific to python3 in another development environment and now need to run it in python3 in this environment.
Any ideas on how to get this working? I've tried updating python also, but I can't get it to upgrade versions.
You have to install scapy library before using it. By what you write it seems you just installed python3 and not scapy. Either:
install pip3 and then pip3 install scapy to install scapy library for all system
(recommended) or use virtualenv and install the same way inside virtual environment, and then run your script inside the python virtual environment
On usage of virutalenv for python you can easily find many guides. Please, comment if you need assistance on that.
I tried to install distribute or setuptools which has to be done prior to installing pip on the Raspberry Pi.
The command used was $ curl http://python-distribute.org/distribute_setup.py | python
However I got the following error. I can copy past the entire terminal script if requested.
[Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/test-easy-install-2082.pth'
Thanks for helping!
I have never using raspberry pi. But from the log can be seen it complain about cannot have permission to modify python package directory. You have to run pip install using root privilege.
This answer is based on what ever operating system you have but one that is specifically Linux based like raspbian you should open up terminal and type in chmod +x file path here. Also for this to work you should be inside where ever the file is stored when you run chmod +x etc... /Desktop/python/ for example would work.
try
sudo curl http://python-distribute.org/distribute_setup.py | python command
some permisssions problem are resolved like this. I'm not sure if this works.