noiling
Posts: 4
Joined: Fri Feb 02, 2018 7:16 pm

problem with gpio

Fri Feb 02, 2018 7:20 pm

in python i always get
i have a raspberry 3

>>> GPIO.setmode(GPIO.BCM)

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
GPIO.setmode(GPIO.BCM)
NameError: name 'GPIO' is not defined

pcmanbob
Posts: 6921
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: problem with gpio

Fri Feb 02, 2018 10:02 pm

Not much actual information in your post but I am guessing you have not got the line

Code: Select all

import RPi.GPIO as GPIO
At the start of you python program
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

noiling
Posts: 4
Joined: Fri Feb 02, 2018 7:16 pm

Re: problem with gpio

Sat Feb 03, 2018 4:34 am

thanks
i did right

now ive got the problem want to save my program
and later edit it
but if i save and open it then there arn the

>>>

in the next line

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

Re: problem with gpio

Sat Feb 03, 2018 10:00 am

The crystal ball is a little hazy this morning, but it seems likely that you are using the IDLE (or IDLE3) IDE and typing into the default Python Shell Window that appears when you start the IDE. This is nothing to do with the use of gpio.

The Python Shell Window gives you an 'immediate' mode or REPL where each line you enter is directly executed by the Python interpreter. This is not an appropriate way to create a python script that you wish to save and rerun.

From the File/New drop down menu option at the top left of the IDE create a new, blank, file which will not have the >>> prompt. Type your script there and then save it.

noiling
Posts: 4
Joined: Fri Feb 02, 2018 7:16 pm

Re: problem with gpio

Sat Feb 03, 2018 10:08 am

ok thanks
now ive got the problem i dont understand what exaktly to import!!! here is my skript

Code: Select all

import Tkinter
from Tkinter import*
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(4, GPIO.Out)

def anzeige(lb):
    if GPIO.input(4):
        lb.config(text="GPIO 4 high")
    else:
        lb.config(text="GPIO 4 low")
    lb.after(100, anzeige, lb)

def an():
    GPIO.output(4, 1)

def aus():
    GPIO.output(4, 0)

root = Tk()
root.title("chris ist der beste superdaed")
Label(root, text="-----").pack()
lb = Label(root, text="an")
lb.pack()
Label(root, text="-----").pack()
Button(root, text="an", width=40, command=an).pack()
Button(root, text="aus", width=40, command=an).pack()
anzeige(lb)
mainloop()


the message i get is


import error, no modules named Tinker
Last edited by noiling on Sat Feb 03, 2018 11:35 am, edited 2 times in total.

noiling
Posts: 4
Joined: Fri Feb 02, 2018 7:16 pm

Re: problem with gpio

Sat Feb 03, 2018 10:11 am

coding seems not to be the problem because another script (i did really code on my own) that works is

Code: Select all

import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(4, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)
print ("hallo")
for n in range(10):
    print (n)
    GPIO.output(4, 0)
    GPIO.output(17, 1)
    sleep(0.5)
    GPIO.output(4, 1)
    GPIO.output(17, 0)
    sleep(0.5)
print ("superdäd ist der beste")
GPIO.output(4,0)
Last edited by noiling on Sat Feb 03, 2018 11:34 am, edited 1 time in total.

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

Re: problem with gpio

Sat Feb 03, 2018 10:17 am

Python relies on indentation for its syntax. Posting plain text in the forum software removes indentation, making the script close to meaningless. Please use the Code tag mark-up provided by the Code button in the top row of the message edit screen. (You can retrospectively modify your existing post.)

Please post the whole of the error message, accurately. There is indeed no standard module named Tinker. It is also important to distinguish between the (different) module names needed for use with Python3 and Python respectively.

pcmanbob
Posts: 6921
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: problem with gpio

Sat Feb 03, 2018 11:27 am

Are you running you code using python 3 by any chance.

python 2 import Tkinter

python 3 import tkinter (note not capitals.)
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
neilgl
Posts: 952
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: problem with gpio

Sat Feb 03, 2018 11:40 am

Probably the issue is that the module is named Tkinter in Python 2 and tkinter in Python 3.
This script a.py will fail if you run it from a terminal

Code: Select all

python3 a.py

Code: Select all

import Tkinter
from Tkinter import*
print ("hello")
ImportError: No module named 'Tkinter'
but b.py is OK

Code: Select all

import tkinter
from tkinter import*
print ("hello")

PingoBags
Posts: 47
Joined: Mon Nov 06, 2017 10:53 pm
Location: NS, Canada

Re: problem with gpio

Sat Feb 03, 2018 12:18 pm

You could try using try except, that way it should work in python 2&3

Code: Select all

try:
  import Tkinter
  from Tkinter import*
except ImportError:
  import tkinter
  from tkinter import*

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

Re: problem with gpio

Sat Feb 03, 2018 12:31 pm

We really need to hear from the OP, @noilng, because none of these replies directly address the error that the OP reported:
the message i get is


import error, no modules named Tinker
The errors of capitalisation and plural indicate that has been transcribed inaccurately, so nothing can be inferred with confidence from the text of the error.

A cut and paste of the entire error feedback would allow us to see what version of Python is being used, and what the content of the live version of the script actually contains.

Return to “Beginners”