anders_w
Posts: 15
Joined: Sat Jul 19, 2014 7:27 am

Statement ok in IDLE shell, but not in script

Wed Aug 20, 2014 9:02 am

Hello!

I'm having problems with my first Python program. To be precise, it's the calling of "time.sleep".

When I call time.sleep in the IDLE shell, it works perfectly. (I started by importing the time module, and setting a variable for the sleep duration).

When it is part of a script, however, both IDLE and the python3 interpreter of the RPi halt the script, say "syntax error" and highlight the word "time" in time.sleep (the first occurrence of the call; in a function definition before the computer even reaches the main program).

Any suggestions as to what might be the problem or how to correct it?

Thanks in advance

SlashWW3
Posts: 3
Joined: Wed Aug 20, 2014 9:23 am

Re: Statement ok in IDLE shell, but not in script

Wed Aug 20, 2014 9:29 am

Hi,

Can you post an example of the code? I use Geany for my editiing purposes. The code should be as follows:

# for importing the time module
import timetime.sleep(2)

# for waiting a set duration.
time.sleep(2)

# a set duration can also be used with a variable
sleeptime = 2
time.sleep(sleeptime)

anders_w
Posts: 15
Joined: Sat Jul 19, 2014 7:27 am

Re: Statement ok in IDLE shell, but not in script

Wed Aug 20, 2014 9:57 am

thank you. I'll post the code when I get home from work.

I've imported the entire time module, in order to be able to get current time for time stamps, so the import statement is just

Code: Select all

import time

SlashWW3
Posts: 3
Joined: Wed Aug 20, 2014 9:23 am

Re: Statement ok in IDLE shell, but not in script

Wed Aug 20, 2014 10:09 am

Sorry for that, my copy and paste seemed to have failed there. It was supposed to be only:

import time

:)

Sleep Mode zZ
Posts: 319
Joined: Sun Aug 19, 2012 5:56 am
Location: Finland

Re: Statement ok in IDLE shell, but not in script

Wed Aug 20, 2014 10:14 am

This works in both Python 2 and Python 3.

Code: Select all

import time
time.sleep(5)
I'm guessing that the syntax error that you get is not because of any fault in the callling of time.sleep() but something is wrong before that call. The syntax error would mean that the interpreter did not expect a function call there because of something in the previous lines. Maybe a problem in indentation?

User avatar
croston
Posts: 711
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: Statement ok in IDLE shell, but not in script

Wed Aug 20, 2014 10:44 am

You've not got a file named time.py or a directory named time in the same directory as your script? If so, Python will import that instead of the real time module.

anders_w
Posts: 15
Joined: Sat Jul 19, 2014 7:27 am

Re: Statement ok in IDLE shell, but not in script

Wed Aug 20, 2014 11:23 am

I managed to access a copy of the script that I had on Dropbox: https://dl.dropboxusercontent.com/u/318 ... atchdog.py

Edit:
No, there is no time-document in the script folder.

Edit #2:
The indentation seems to be a plausible explanation. It differs slightly depending on what software one uses to view the script. I'll take a closer look at it when I get home.
Last edited by anders_w on Wed Aug 20, 2014 11:39 am, edited 1 time in total.

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

Re: Statement ok in IDLE shell, but not in script

Wed Aug 20, 2014 11:34 am

croston wrote:You've not got a file named time.py or a directory named time in the same directory as your script? If so, Python will import that instead of the real time module.
I do not see how that could give a syntax error highlighting time? Although, I struggle to find any way to generate a SyntaxError in the first word of a line.

The posted code is littered with typos and would be better not starting with #!/bin/sh! (I will assume the BOM was added by dropbox.) None of the errors appear to relate to time.sleep, however.

anders_w
Posts: 15
Joined: Sat Jul 19, 2014 7:27 am

Re: Statement ok in IDLE shell, but not in script

Wed Aug 20, 2014 11:41 am

jojopi wrote:The posted code is littered with typos and would be better not starting with #!/bin/sh! (I will assume the BOM was added by dropbox.) None of the errors appear to relate to time.sleep, however.
I'm not surprised at the typos, since it's my first venture into this programming language. The first few lines were added automatically by Xcode (Apple Mac OS X), the environment where I write the code.

anders_w
Posts: 15
Joined: Sat Jul 19, 2014 7:27 am

Re: Statement ok in IDLE shell, but not in script

Wed Aug 20, 2014 11:44 am

By the way: Is it possible to use some sort of shell script to make the RPi start the script automatically whenever the RPi boots up?
The RPi uses Raspbian, and I am not sure how to make a script "self-starting" at boot-up.

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

Re: Statement ok in IDLE shell, but not in script

Wed Aug 20, 2014 11:58 am

anders_w wrote:I'm not surprised at the typos, since it's my first venture into this programming language. The first few lines were added automatically by Xcode (Apple Mac OS X), the environment where I write the code.
Sorry, I was not meaning to criticise the typos. I was pointing out that that code does not appear to give the error you are asking about. It gives a lot of different errors instead.

Incidentally, are you sure the problem is a SyntaxError, not a NameError or IndentationError?

anders_w
Posts: 15
Joined: Sat Jul 19, 2014 7:27 am

Re: Statement ok in IDLE shell, but not in script

Wed Aug 20, 2014 1:10 pm

No offense taken.

Yes, both IDLE (runtime) and RPi python3 say "SyntaxError"

DirkS
Posts: 10389
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Statement ok in IDLE shell, but not in script

Wed Aug 20, 2014 2:39 pm

anders_w wrote:By the way: Is it possible to use some sort of shell script to make the RPi start the script automatically whenever the RPi boots up?
The RPi uses Raspbian, and I am not sure how to make a script "self-starting" at boot-up.
There are several ways of doing that. Adding a line in /etc/rc.local, using cron with the 'time' @reboot, and more.

I'm starting a python script in rc.local like this:

Code: Select all

#!/bin/sh -e
/home/pi/scripts/raspiserver-status.py &
exit 0
You need to put in commands before 'exit 0'; the '&' at the end of the line starts the script in the background.
See also e.g. http://www.raspberrypi.org/forums/viewt ... 63&t=46125; there are quite a few posts about this on this forum (and elsewhere)

Gr.
Dirk.

dannym
Posts: 7
Joined: Thu Jul 19, 2012 9:01 pm

Re: Statement ok in IDLE shell, but not in script

Sat Aug 23, 2014 8:45 pm

I doubt that Dropbox added the BOM. Probably was XCode. In any case, there seem to be CR characters in the file (and shouldn't be. They can really mess up legibility of error messages and also Linux tries to start an application with CR in the name).

anders_w, what's up with the /bin/sh ? That should be /usr/bin/python -- or /usr/bin/python3 -- or something. That isn't just nitpicking, that is where Linux looks for what program to start to execute your file. It better be correct...

You can check your copy of the file for weird characters by

Code: Select all

od -c Watchdog.py | less
(exit by q)

These should not appear anywhere in it:

Code: Select all

357 273 277
and

Code: Select all

\r
Problems like this one go away when you create it directly on the Pi. nano for example is a very easy-to-use editor.
Otherwise, you can use dos2unix or recode to convert it to unix format, if you must.
Also, you'll still get an indentation error because it's messed up at three places.
Is XCode that bad? I thought it was the primary professional mac developer ide?

Return to “Python”