Taz2108
Posts: 9
Joined: Mon Feb 06, 2017 10:33 am

Syntax error on import time

Fri Dec 29, 2017 11:14 am

I have tried the following config with other configs, but keep getting syntax error, config and error copied below, could someone kindly provide suggestions. Thanks in advance

import RPi.GPIO as GPIO
import time
pin = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)
time.sleep(1)
GPIO.output(pin, GPIO.LOW)
time.sleep(1)
GPIO.cleanup()



Traceback (most recent call last):
File "/home/pi/Desktop/RPI3.py", line 1, in <module>
import RPi.GPIO as GPIO ## Import GPIO Library
File "/home/pi/Desktop/RPi.py", line 2
>>> import time
^
SyntaxError: invalid syntax

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

Re: Syntax error on import time

Fri Dec 29, 2017 11:49 am

Your program runs fine on my pi.

I suspect you may have a hidden character like a windows line feed on one of the first 2 lines .

how did you enter the code ?

was it copied from a website by any chance.

try editing the file directly on the pi or via ssh using nano and delete any characters on the end of each line.

copy of working program

Code: Select all

import RPi.GPIO as GPIO        
import time
pin = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT) 
GPIO.output(pin, GPIO.HIGH)
time.sleep(1)
GPIO.output(pin, GPIO.LOW)
time.sleep(1)
GPIO.cleanup() 

We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Taz2108
Posts: 9
Joined: Mon Feb 06, 2017 10:33 am

Re: Syntax error on import time

Fri Dec 29, 2017 8:38 pm

Thanks for the response
I have tried what you suggested and also tried SSH, used nano to enter the config and tested it through SSH and got the same error on import time.
Could it be that I need to add "import time" on the library and if I do please let me know how to do this or any other suggestions you may have. Thanks

User avatar
jojopi
Posts: 3063
Joined: Tue Oct 11, 2011 8:38 pm

Re: Syntax error on import time

Fri Dec 29, 2017 9:09 pm

Taz2108 wrote:
Fri Dec 29, 2017 11:14 am
File "/home/pi/Desktop/RPi.py", line 2
>>> import time
^
SyntaxError: invalid syntax
There are three greater-than signs at the start of the line. ">>> import time" is a syntax error.

I understand this problem can arise if you type your commands into an interactive IDLE3 window, and then save the transcript as a file. You should open a new file window first and paste into that and save it.

Alternatively, load your "RPI.py" file into any editor and remove any prompts or other extraneous text.

Taz2108
Posts: 9
Joined: Mon Feb 06, 2017 10:33 am

Re: Syntax error on import time

Sat Dec 30, 2017 4:26 pm

I have tried what you have suggested. Have also used notepad++ file editor and pasted the config on the idle screen from the editor, this gives same error.
have also tried saving word docs on PC and then transferring these to PI, same problem.
To relay any issues with copy and paste, have created config directly on notepad++ file editor, saved this on the computer and transferred file by using FireZilla directly to PI, again having same issue when I run the file.
Will be grateful for any other suggestions.

User avatar
Jednorozec
Posts: 809
Joined: Sun Nov 24, 2013 2:17 pm
Location: Deposit, NY

Re: Syntax error on import time

Sat Dec 30, 2017 5:18 pm

Edit the file on the RPi with nano.
The most important leg of a three legged stool is the one that's missing.
It's called thinking. Why don't you try it sometime?

Taz2108
Posts: 9
Joined: Mon Feb 06, 2017 10:33 am

Re: Syntax error on import time

Sat Dec 30, 2017 8:47 pm

Tried this tried and tried again by opening file in nano from the one saved in idle, also type the contents in nano with new file name also by editing the name of file and name, unfortunately the same syntax.

User avatar
Jednorozec
Posts: 809
Joined: Sun Nov 24, 2013 2:17 pm
Location: Deposit, NY

Re: Syntax error on import time

Sat Dec 30, 2017 8:57 pm

What happens if you run python and type

Code: Select all

import time
The most important leg of a three legged stool is the one that's missing.
It's called thinking. Why don't you try it sometime?

User avatar
jojopi
Posts: 3063
Joined: Tue Oct 11, 2011 8:38 pm

Re: Syntax error on import time

Sat Dec 30, 2017 9:32 pm

Taz2108 wrote:
Fri Dec 29, 2017 11:14 am
File "/home/pi/Desktop/RPI3.py", line 1, in <module>
import RPi.GPIO as GPIO ## Import GPIO Library
File "/home/pi/Desktop/RPi.py", line 2
>>> import time
^
SyntaxError: invalid syntax
The extra ">>>" are the file Desktop/RPi.py. This is not the file you are running. You are importing it by accident, because it is in the same directory as RPI3.py, which says "import RPi.GPIO".

Taz2108
Posts: 9
Joined: Mon Feb 06, 2017 10:33 am

Re: Syntax error on import time

Sun Dec 31, 2017 10:21 am

Thanks - Please refer to:

1. Raspberry – Created new Directory; Test_Sen
[email protected]:~ $ pwd
/home/pi

[email protected]:~ $ ls
Desktop hello.py.save Pictures Public RPi.py
Documents Music PIR.py.save python_games Templates
Downloads my_files PRi.py RPI14 Test_Sen
hello.py oldconffiles PRI.py RPi30.py Videos
[email protected]:~ $


2. Idle – Run Config – have created new folder Test_Sen and changed name of file Test_Two.py, but the test still goes to RPi.py as before, is there a preferred method of creating new directory?

Traceback (most recent call last):
File "/home/pi/Test_Sen/Test_Two.py", line 1, in <module>
import RPi.GPIO as GPIO
File "/home/pi/RPi.py", line 2
>>> import time
^
SyntaxError: invalid syntax
>>>

3. Raspberry – Import Time – Import time Command not found
[email protected]:/ $ pwd
/
[email protected]:/ $ import time
-bash: import: command not found

4. Raspberry Pi – added config through nano – no more import time error – It goes straight back to Raspberry Prompt – The only thing different done is using nano with new folder Test_Sen

[email protected]:~/Test_Sen $ ls
Test_One.py Test_Two.py

[email protected]:~/Test_Sen $ python Test_Two.py
[email protected]:~/Test_Sen $

User avatar
DougieLawson
Posts: 35118
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Syntax error on import time

Sun Dec 31, 2017 10:33 am

Stop using IDLE, switch to Thonny as I think it will be easier for you to understand it.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a hippy & doctor free zone.

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

Re: Syntax error on import time

Sun Dec 31, 2017 12:27 pm

Ok seeing as you are having so much trouble with this, try doing this.

Either use a terminal window or log on to your pi via ssh.

then type this at the command line.

Code: Select all


nano testme.py

then in the new file that nano has opened type theses lines

Code: Select all


import time
print ("hello")
print (time.strftime("%H:%M:%S"))

then save and close nano

then at the command line type

Code: Select all

python testme.py

make sure follow the instructions and use the name testme.py for the file name.

this should work and print hello and the current time.

if it all works as it should, I suggest you stop using RPI in your python file names.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
jojopi
Posts: 3063
Joined: Tue Oct 11, 2011 8:38 pm

Re: Syntax error on import time

Sun Dec 31, 2017 1:02 pm

Taz2108 wrote:
Sun Dec 31, 2017 10:21 am
2. Idle – Run Config – have created new folder Test_Sen and changed name of file Test_Two.py, but the test still goes to RPi.py as before, is there a preferred method of creating new directory?
This does seem to be another Idle quirk. Normally a local file will override a system module only if it is in the same directory as the script you are running.

However, compared to running the script directly, Idle's sys.path includes extra entries for /usr/bin (!), the absolute directory where Idle was started from, and the current working directory. This is actually quite dangerous, because if you run Idle in a public directory like /tmp, you could end up importing code written by another user.

Anyway the solution for you is not to have any files called RPi.py if you plan to "import RPi.GPIO".

Taz2108
Posts: 9
Joined: Mon Feb 06, 2017 10:33 am

Re: Syntax error on import time

Sun Dec 31, 2017 5:16 pm

1. The config provided by you below is working fine

nano testme.py
import time
print ("hello")
print (time.strftime("%H:%M:%S"))


2. My original Config Test

I have deleted all the files with RPi, this has solved the problem with syntax error with import time, as I have used a different file names.

a) On SSH when testing the config below it goes back to command prompt
[email protected]:~/Test_Sen $ python Test_One.py
[email protected]:~/Test_Sen $

The config used is:

import RPi.GPIO as GPIO
import time
pin = 7
GPIO.setmode(GPIO.BOARD)
GPIO.setmode(pin,GPIO.OUT)
GPIO.output(pin,GPIO.HIGH)
time.sleep(1)
GPIO.out(pin,GPIO.LOW)
time.sleep(1)
GPIO.cleanup()


a) Test on Idle for above config again no error, but no results
Python 2.7.9 (default, Sep 17 2016, 20:26:04)
[GCC 4.9.2] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
>>>


3. Maybe the config is not suitable for RPI sensor
If you think the config is not suitable would you have another config to get the RPI sensor working.
We are getting there ! thanks guys

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

Re: Syntax error on import time

Sun Dec 31, 2017 5:40 pm

What results are you expecting ?

All you script does is set gpio pin7 ( note you are using board mode so this is actually gpio 4)
To output and then drive it high for 1 second, then drive it low for 1 second.
There is no output to the screen so nothing to be displayed.

If you are not seeing the output on the gpio you expect check you are looking at the right gpio pin.

It might help if you explain what sensor you are trying to connect to your pi.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Taz2108
Posts: 9
Joined: Mon Feb 06, 2017 10:33 am

Re: Syntax error on import time

Sun Dec 31, 2017 8:34 pm

PIR Motion Sensor Module - HC-SR501
https://thepihut.com/products/pir-motion-sensor-module
is there a simple config I can use with this sensor to display something?

Have a happy new year everyone

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

Re: Syntax error on import time

Sun Dec 31, 2017 10:28 pm

To connect your PIR to you gpio you need to set the gpio pin to input not output.

So you need to connect your sensor up in the following way.

5V to + pin on PIR ( on most devices right pin )
Ground to Ground pin on PIR( on most devices left pin )
GPIO 18 ( pin 12 ) to output pin on PIR ( always middle pin )

for ref
Image

Then you need to run this code.( python 2 code )

Code: Select all

import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18, GPIO.IN)

while True:
    #wait for pir to trigger.
    print "Waiting for motion"
    while GPIO.input(18) == 0:
        time.sleep(0.5)
        
    # once motion detected print and then wait for motion to stop.
    print "Motion Detected"
    while GPIO.input(18) == 1:
        time.sleep(0.5)    
 
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Taz2108
Posts: 9
Joined: Mon Feb 06, 2017 10:33 am

Re: Syntax error on import time

Mon Jan 01, 2018 11:31 am

Thanks pcmanbob, excellent all working

Return to “General discussion”