Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Rpi don't install

Sun Jun 12, 2016 11:19 am

Hello, i use python 3.4 and have error: I installed rpi:
sudo pip3 install RPi.GPIO
, but when i start: sudo led.py
I have error:

Code: Select all

import RPi.GPIO as GPIO
ImportError: No module named 'RPi'
How resolve it?

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Rpi don't install

Sun Jun 12, 2016 11:37 am

What is the first [ #! ] line of led.py?

And are you using a very old release of the Operating System for your RPi? Because RPi.GPIO has been part of the standard tools included with the OS for ages, you shouldn't need to install it yourself.

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Rpi don't install

Sun Jun 12, 2016 11:44 am

The first lines:

Code: Select all

import time
import RPi.GPIO as GPIO
 
GPIO.setmode(GPIO.BCM)
I use Raspabian Jessie

dgordon42
Posts: 788
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Rpi don't install

Sun Jun 12, 2016 12:39 pm

To install RPi.GPIO for Python3 try:

Code: Select all

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3-rpi.gpio
and follow the prompts.

These commands will update your Pi to the latest version of Raspbian, and install RPi.GPIO for Python 3.

Hope this helps,
Dave.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Rpi don't install

Sun Jun 12, 2016 12:43 pm

I don't think we've got the full story here.

If I try to use RPi.GPIO directly from the Python interpreter REPL I see:

Code: Select all

pi@rpi2b ~/ivan $ python3
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO as GPIO
>>> GPIO.setmode(GPIO.BCM)
>>>
Here is my version of your code:

Code: Select all

pi@rpi2b ~/ivan $ cat led.py
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

print("Import test completed OK.")

It runs correctly if the python3 interpreter is invoked directly:

Code: Select all

pi@rpi2b ~/ivan $ python3 led.py
Import test completed OK.
If I try to invoke it directly with sudo - which is in any case superfluous - I get a different error:

Code: Select all

pi@rpi2b ~/ivan $ sudo led.py
sudo: led.py: command not found
Trying to specify the file in my working directory fails too:

Code: Select all

pi@rpi2b ~/ivan $ sudo ./led.py
./led.py: 1: ./led.py: import: not found
./led.py: 2: ./led.py: import: not found
./led.py: 4: ./led.py: Syntax error: word unexpected (expecting ")")
If I put a #! line at the top of the script it works correctly:

Code: Select all

pi@rpi2b ~/ivan $ cat led2.py
#!/usr/bin/python3
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

print("Import test completed OK.")

pi@rpi2b ~/ivan $ sudo ./led2.py
Import test completed OK.
I can't recreate your failure mode: what are you doing differently?

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Rpi don't install

Sun Jun 12, 2016 1:36 pm

You're missing a shebang line.

Add

Code: Select all

#!/usr/bin/env python
Then run with sudo ./led.py

Or run with sudo python ./led.py
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Rpi don't install

Sun Jun 12, 2016 2:14 pm

Where I must add this line?
DougieLawson wrote:You're missing a shebang line.

Add

Code: Select all

#!/usr/bin/env python
Then run with sudo ./led.py

Or run with sudo python ./led.py

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: Rpi don't install

Sun Jun 12, 2016 2:15 pm

Ivan219 wrote:Where I must add this line?
DougieLawson wrote:You're missing a shebang line.

Add

Code: Select all

#!/usr/bin/env python
Then run with sudo ./led.py

Or run with sudo python ./led.py
To the top on a new line.
There are 10 types of people: those who understand binary and those who don't.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Rpi don't install

Sun Jun 12, 2016 2:18 pm

Ivan219 wrote:Where I must add this line?
You could read my reply, where I give an example of using this.

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Rpi don't install

Sun Jun 12, 2016 3:59 pm

I tried:

Code: Select all

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3-rpi.gpio
And i got:
The following packages have unmet dependencies:
python3-rpi.gpio : Depends: python3 (< 3.3) but 3.4.2-2 is to be installed
E: Unable to correct problems, you have held broken packages.
Also i tried install via pip3 and it was successful, but if i even do:
pi@raspberrypi ~ $ sudo python3
Python 3.4.2 (default, Jun 7 2016, 13:30:50)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO as GPIO
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'RPi'
After uninstall via pip3 and install again, i got this again

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Rpi don't install

Sun Jun 12, 2016 4:07 pm

And with:
#!/usr/bin/python3
or
#!/usr/bin/env python
i have the same error

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Rpi don't install

Sun Jun 12, 2016 4:53 pm

I think, that problem in python 3.4... Can i delete him and install it again?

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Rpi don't install

Sun Jun 12, 2016 5:53 pm

I haven't any modules on python3.4, but they in dist-packages

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Rpi don't install

Sun Jun 12, 2016 5:58 pm

Yes, if I start program from root-user and in idle - all is working. But in console - errors, and in pi-user in idle i have permission (i know that this in normal). How I can resolve, that my programm is working in console?
Sometime ago I made this:

Code: Select all

rm /usr/bin/python3
ln -s /usr/local/bin/python3 /usr/bin/
ln -s /usr/local/bin/pip3 /usr/bin/
May be error in this? How delete it?

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Rpi don't install

Sun Jun 12, 2016 6:08 pm

I had two python3.4 I delete some files from python3.4 (downloaded), and in console I am getting errors, that i haven't some files.. I want to delete python3.4 (downloaded), and save python3.4 (from OS)

dgordon42
Posts: 788
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Rpi don't install

Sun Jun 12, 2016 6:47 pm

Remember this thread?
I was trying to determine from you how you came to have several versions of Python on your Pi. The commands you used like:
Ivan219 wrote:rm /usr/bin/python3
ln -s /usr/local/bin/python3 /usr/bin/
ln -s /usr/local/bin/pip3 /usr/bin/
won't properly remove or delete python, they will however break your Python installation making it difficult to add, remove, or get anything to work.

Try the following commands to completely remove all versions of Python3, RPi.GPIO and pip3 from your Pi.

Code: Select all

sudo apt-get purge python3-pip
sudo apt-get purge python3-rpi.gpio
sudo apt-get purge python3*
Follow the prompts and report any error messages.
Hopefully, this will get rid your damaged Python3 installation.
Reboot.

Get your Pi up to date with:

Code: Select all

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
Follow the prompts and report any error messages
Reboot.

Install Python3 with:

Code: Select all

sudo apt-get install python3
Report any messages from apt-get.

Install RPi.GPIO for Python3 with:

Code: Select all

sudo apt-get install python3-rpi.gpio
Report any messages from apt-get

Copy and save the following program to your home directory as "testing+RPI+P3.py"

Code: Select all

#!/usr/bin/env python3
import RPi.GPIO as GPIO
print(GPIO.VERSION)
Make the file executable with the command:

Code: Select all

chmod 744  testing+RPI+P3.py
Run the program from your home directory with:

Code: Select all

./testing+RPI+P3.py
Report the output.

If you cannot get this to work, I would advise that you back up any data you have on the SD card, and re install Raspbian.

Hope this helps,
Dave.

Ivan219
Posts: 118
Joined: Sun Jul 05, 2015 10:01 pm

Re: Rpi don't install

Sun Jun 12, 2016 8:51 pm

After

Code: Select all

sudo apt-get purge python3*
deleted GUI, and after reboot too... I made bakcup before this. And I think that I could reinstall raspabian...

Return to “Beginners”