FAQ
I have a trivially simple piece of code called timewaster.py:
____________________________________________________

while True:
i = 0
for i in range(10):
break
_____________________________________________________

It runs fine with Eric but when I try to run it from shell...
./timewaster.py
./timewaster.py: line 4: syntax error near unexpected token `('
./timewaster.py: line 4: ` for i in range(10):'

I've tried this on openSuse 11.3 and Kubuntu 10.04, both use Python
version 2.6.5, both show the above.

Before I tear out my hair any more (only 3 left) I thought I'd ask here
what I am doing wrong.

Best Regards
Werner Dahn

Search Discussions

  • Nitin Pawar at Feb 17, 2011 at 8:37 am
    can you share the first line of your shell (shabang) ?

    I think you have forgotten to tell the shell which interpreter to use

    if you have not put #!/usr/bin/python then its plain shell script which is
    incorrect


    On Thu, Feb 17, 2011 at 1:57 PM, Werner wrote:

    I have a trivially simple piece of code called timewaster.py:
    ____________________________________________________

    while True:
    i = 0
    for i in range(10):
    break
    _____________________________________________________

    It runs fine with Eric but when I try to run it from shell...
    ./timewaster.py
    ./timewaster.py: line 4: syntax error near unexpected token `('
    ./timewaster.py: line 4: ` for i in range(10):'

    I've tried this on openSuse 11.3 and Kubuntu 10.04, both use Python
    version 2.6.5, both show the above.

    Before I tear out my hair any more (only 3 left) I thought I'd ask here
    what I am doing wrong.

    Best Regards
    Werner Dahn
    --
    http://mail.python.org/mailman/listinfo/python-list


    --
    Nitin Pawar
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20110217/7c988cab/attachment.html>
  • Chris Rebert at Feb 17, 2011 at 8:39 am

    On Thu, Feb 17, 2011 at 12:27 AM, Werner wrote:
    I have a trivially simple piece of code called timewaster.py:
    ____________________________________________________

    while True:
    ? ?i = 0
    ? ?for i in range(10):
    ? ? ? ?break
    _____________________________________________________

    It runs fine with Eric but when I try to run it from shell...
    ./timewaster.py
    ./timewaster.py: line 4: syntax error near unexpected token `('
    ./timewaster.py: line 4: ` ? ?for i in range(10):'

    I've tried this on openSuse 11.3 and Kubuntu 10.04, both use Python
    version 2.6.5, both show the above.

    Before I tear out my hair any more (only 3 left) I thought I'd ask here
    what I am doing wrong.
    Looks like it's being run as a shell script rather than through the
    Python interpreter (hence why the error is not in the form of an
    exception with a traceback).

    Try adding:

    #!/usr/bin/env python

    as the first line in your file. This tells the shell to run the script
    using Python.

    Or alternatively, instead of:

    ./timewaster.py

    use:

    python timewaster.py

    which likewise explicitly invokes the Python interpreter.

    Cheers,
    Chris
  • Werner at Feb 17, 2011 at 8:42 am

    On 17/02/11 16:39, Chris Rebert wrote:
    On Thu, Feb 17, 2011 at 12:27 AM, Werner wrote:
    I have a trivially simple piece of code called timewaster.py:
    ____________________________________________________

    while True:
    i = 0
    for i in range(10):
    break
    _____________________________________________________

    It runs fine with Eric but when I try to run it from shell...
    ./timewaster.py
    ./timewaster.py: line 4: syntax error near unexpected token `('
    ./timewaster.py: line 4: ` for i in range(10):'

    I've tried this on openSuse 11.3 and Kubuntu 10.04, both use Python
    version 2.6.5, both show the above.

    Before I tear out my hair any more (only 3 left) I thought I'd ask here
    what I am doing wrong.
    Looks like it's being run as a shell script rather than through the
    Python interpreter (hence why the error is not in the form of an
    exception with a traceback).

    Try adding:

    #!/usr/bin/env python

    as the first line in your file. This tells the shell to run the script
    using Python.

    Or alternatively, instead of:

    ./timewaster.py

    use:

    python timewaster.py

    which likewise explicitly invokes the Python interpreter.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Yes, that was it. AYAA!

    Thank you very much.
  • Alister Ware at Feb 17, 2011 at 6:40 pm

    On Thu, 17 Feb 2011 16:42:05 +0800, Werner wrote:
    On 17/02/11 16:39, Chris Rebert wrote:
    On Thu, Feb 17, 2011 at 12:27 AM, Werner wrote:
    I have a trivially simple piece of code called timewaster.py:
    ____________________________________________________

    while True:
    i = 0
    for i in range(10):
    break
    _____________________________________________________

    It runs fine with Eric but when I try to run it from shell...
    ./timewaster.py
    ./timewaster.py: line 4: syntax error near unexpected token `('
    ./timewaster.py: line 4: ` for i in range(10):'

    I've tried this on openSuse 11.3 and Kubuntu 10.04, both use Python
    version 2.6.5, both show the above.

    Before I tear out my hair any more (only 3 left) I thought I'd ask
    here what I am doing wrong.
    Looks like it's being run as a shell script rather than through the
    Python interpreter (hence why the error is not in the form of an
    exception with a traceback).

    Try adding:

    #!/usr/bin/env python

    as the first line in your file. This tells the shell to run the script
    using Python.

    Or alternatively, instead of:

    ./timewaster.py

    use:

    python timewaster.py

    which likewise explicitly invokes the Python interpreter.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Yes, that was it. AYAA!

    Thank you very much.
    may I ask what is the purpose of this code segment, it does not look like
    it would achieve much?




    --
    All that glitters is not gold; all that wander are not lost.
  • Cameron Simpson at Feb 17, 2011 at 9:40 pm

    On 17Feb2011 18:40, Alister Ware wrote: | On Thu, 17 Feb 2011 16:42:05 +0800, Werner wrote:
    On 17/02/11 16:39, Chris Rebert wrote:
    On Thu, Feb 17, 2011 at 12:27 AM, Werner wrote:
    I have a trivially simple piece of code called timewaster.py:
    ____________________________________________________
    while True:
    [...]
    It runs fine with Eric but when I try to run it from shell...
    ./timewaster.py
    ./timewaster.py: line 4: syntax error near unexpected token `('
    ./timewaster.py: line 4: ` for i in range(10):'
    [...]
    Looks like it's being run as a shell script rather than through the
    Python interpreter (hence why the error is not in the form of an
    exception with a traceback).

    Try adding:
    #!/usr/bin/env python

    as the first line in your file. This tells the shell to run the script
    using Python.
    [...]
    may I ask what is the purpose of this code segment, it does not look like
    it would achieve much?
    When you run a command from the shell, it first tries to directly execv()
    it, which calls the kernel to load and execute the file. If the file is
    not a kernel compatibly file, the shell then presumes the file is a
    shell script and interprets it as shell commands.

    Thus the original error message, since it is Python code and not shell
    code.

    The shebang line:

    #!/usr/bin/env python

    is a special piece of script syntax recognised _by_the_kernel_, which
    makes the script something the kernel will handle. If a file's first two
    characters are:

    #!

    then the kernel reads the remainder of that line as a command to be used
    to execute the script by appending the script pathname. So the line
    supplied causes the kernel to execute the command:

    /usr/bin/env python the-path-to-the-script ...

    where "..." would be any additional arguments you supplied on the
    command line. In this way, many scripting languages can be "directly"
    executed.

    These two characters were chosen because "#" is a comment character in
    many/most UNIX scripting languages and because "!" is the traditional
    UNIX indicator for a "shell escape" it tools like editors, where one
    indicates that one wants to run an external shell comand by preceeding
    it with a "!" "(bang"), eg:

    ! ls

    and variations.

    Now, it is usual to provide the direct path to the interpreter, eg:

    #!/bin/sh
    #!/usr/bin/python

    etc. However, some some things that path may vary from system to system.
    On POSIX systems, /bin/sh is guarenteed to be present at that location,
    but python or perl may be in /usr/bin or /usr/local/bin etc.
    The #! syntax _requires_ an absolute path to the executable.
    If the executable may be in different places (because the script author
    is shipping a script that will run on some unknown system) this is a
    problem.

    The "env" command is actually a special hook for setting environment
    variables before running a command, but without extra arguments is can
    be used to rely on $PATH to find the command. So the command:

    /usr/bin/env python

    incantation finds "the python that would normally be run", without
    needing to know the install path of the python executable.

    Cheers,
    --
    Cameron Simpson <cs at zip.com.au> DoD#743
    http://www.cskk.ezoshosting.com/cs/

    A common criticism of the Internet is that it is dominated by the crude, the
    uninformed, the immature, the smug, the untalented, the repetitious, the
    pathetic, the hostile, the deluded, the self-righteous, and the shrill. This
    criticism overlooks the fact that the Internet also offers -- for the savvy
    individual who knows where to look -- the tasteless and the borderline insane.
    - _A brief History of Computing #12_ Heck <jim at comms2.calstate.edu>
  • Cameron Simpson at Feb 17, 2011 at 11:29 pm

    On 18Feb2011 08:40, I wrote: | On 17Feb2011 18:40, Alister Ware wrote:
    On Thu, 17 Feb 2011 16:42:05 +0800, Werner wrote:
    On 17/02/11 16:39, Chris Rebert wrote:
    On Thu, Feb 17, 2011 at 12:27 AM, Werner wrote:
    I have a trivially simple piece of code called timewaster.py:
    ____________________________________________________
    while True:
    [...]
    may I ask what is the purpose of this code segment, it does not look like
    it would achieve much?
    [... long shebang description...]

    Hmm, looks like maybe you meant the original python snippet.
    If so, my apologies for the long misanswer.
    --
    Cameron Simpson <cs at zip.com.au> DoD#743
    http://www.cskk.ezoshosting.com/cs/

    I was gratified to be able to answer promptly and I did.
    I said I didn't know. - Mark Twain
  • Werner at Feb 18, 2011 at 5:48 pm

    On 18/02/11 07:29, Cameron Simpson wrote:
    On 18Feb2011 08:40, I wrote:
    On 17Feb2011 18:40, Alister Ware wrote:
    On Thu, 17 Feb 2011 16:42:05 +0800, Werner wrote:
    On 17/02/11 16:39, Chris Rebert wrote:
    On Thu, Feb 17, 2011 at 12:27 AM, Werner wrote:
    I have a trivially simple piece of code called timewaster.py:
    ____________________________________________________
    while True:
    [...]
    may I ask what is the purpose of this code segment, it does not look like
    it would achieve much?
    [... long shebang description...]

    Hmm, looks like maybe you meant the original python snippet.
    If so, my apologies for the long misanswer.
    It was't a misanswer, you were laying a finger on the wound and
    explained how things are. I am very grateful for that.

    This is my first contact with this newsgroup and I thank you all for
    being patient and understanding with a newbie.

    Best Regards
    Werner Dahn
  • Werner at Feb 17, 2011 at 11:10 pm

    On 18/02/11 02:40, Alister Ware wrote:
    On Thu, 17 Feb 2011 16:42:05 +0800, Werner wrote:
    On 17/02/11 16:39, Chris Rebert wrote:
    On Thu, Feb 17, 2011 at 12:27 AM, Werner wrote:
    I have a trivially simple piece of code called timewaster.py:
    ____________________________________________________

    while True:
    i = 0
    for i in range(10):
    break
    _____________________________________________________

    It runs fine with Eric but when I try to run it from shell...
    ./timewaster.py
    ./timewaster.py: line 4: syntax error near unexpected token `('
    ./timewaster.py: line 4: ` for i in range(10):'

    I've tried this on openSuse 11.3 and Kubuntu 10.04, both use Python
    version 2.6.5, both show the above.

    Before I tear out my hair any more (only 3 left) I thought I'd ask
    here what I am doing wrong.
    Looks like it's being run as a shell script rather than through the
    Python interpreter (hence why the error is not in the form of an
    exception with a traceback).

    Try adding:

    #!/usr/bin/env python

    as the first line in your file. This tells the shell to run the script
    using Python.

    Or alternatively, instead of:

    ./timewaster.py

    use:

    python timewaster.py

    which likewise explicitly invokes the Python interpreter.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Yes, that was it. AYAA!

    Thank you very much.
    may I ask what is the purpose of this code segment, it does not look like
    it would achieve much?


    It is meant to put load on a CPU, RAM and disk (swap). The code now
    looks like this:
    #!/usr/bin/python
    while True:
    i = 0
    for i in range(20000000):
    break

    I needed this to get an idea how virtual machines inside a host perform
    when you run a few instances of it.

    The objective was at what point responsiveness gets severely degraded by
    load. To measure that I used
    __________________________________
    #!/usr/bin/python
    import time

    def time_it(threshold , period):
    t0 = time.time() # daytime at start of period
    time.sleep(period)
    t1 = time.time() # daytime at end of period
    t2 = t1-t0 # calculate actual lenght of period
    # subtract how long it should have been
    t3 =(t2 - period) *1000
    if t3>=threshold: # if the differnce is too high report it
    print time.strftime('%Y/%m/%d %H:%M:%S ') , round(t3, 2), "ms"


    while True:
    time_it(5.0, 0.1)
    ___________________________________

    Not very sophisticated but it gave me a few clues of what I'm interested in.

    Regards
    Werner Dahn
  • Michael Torrie at Feb 18, 2011 at 4:54 pm

    On 02/17/2011 04:10 PM, Werner wrote:
    It is meant to put load on a CPU, RAM and disk (swap). The code now
    looks like this:
    #!/usr/bin/python
    while True:
    i = 0
    for i in range(20000000):
    break
    Just for your information, your code is the equivalent of:

    while True:
    temp = range(20000000)

    The for loop does absolutely nothing in your case. After the range is
    computed, the for loop exits on the first iteration.

    On python 3.0 range() is a generator so your code essentially distills
    down to this if you ran it on python 3:

    while True:
    pass
  • Noah Hall at Feb 18, 2011 at 5:06 pm

    On Fri, Feb 18, 2011 at 4:54 PM, Michael Torrie wrote:
    On 02/17/2011 04:10 PM, Werner wrote:
    Just for your information, your code is the equivalent of:

    while True:
    ? ?temp = range(20000000)

    The for loop does absolutely nothing in your case. ?After the range is
    computed, the for loop exits on the first iteration.
    Ah, but the for loop is there for a reason, I imagine. It'd certainly
    be slower per while loop.
  • Larry Hudson at Feb 18, 2011 at 4:15 am

    On 02/17/2011 12:27 AM, Werner wrote:
    I have a trivially simple piece of code called timewaster.py:
    ____________________________________________________

    while True:
    i = 0
    for i in range(10):
    break
    _____________________________________________________

    It runs fine with Eric but when I try to run it from shell...
    ./timewaster.py
    ./timewaster.py: line 4: syntax error near unexpected token `('
    ./timewaster.py: line 4: ` for i in range(10):'

    I've tried this on openSuse 11.3 and Kubuntu 10.04, both use Python
    version 2.6.5, both show the above.

    Before I tear out my hair any more (only 3 left) I thought I'd ask here
    what I am doing wrong.

    Best Regards
    Werner Dahn
    A true time waster indeed -- it's an infinite loop that will _never_ end.

    Others have already about the need of the shebang line to run as a python script, but I'm
    surprised no one mentioned how truly useless this code is.

    The i = 0 line is totally unnecessary. The variable i is created and set to zero by the first
    iteration of the for loop. The break will abort the for loop (NOT the while loop) in the first
    iteration, and the 2nd through the 10th iterations will be skipped altogether.

    This effectively leaves your code as:

    while True:
    pass # Do nothing, forever

    An empty loop as a time delay can occasionally be useful, but as a practical matter, a for loop
    with only 10 (empty/pass) iterations is probably too short for anything useful. Instead of
    being empty, do some more complex (but ignored) operation -- say math.sqrt() or math.sin() for
    example -- and a much larger repetition count. But it's likely that does use up processor
    cycles unnecessarily, although it can give you delays of fractions of seconds. If you want
    delays greater than a second, check out the time.sleep() function.

    -=- Larry -=-
  • Michael Torrie at Feb 18, 2011 at 4:55 pm

    On 02/17/2011 09:15 PM, Larry Hudson wrote:
    A true time waster indeed -- it's an infinite loop that will _never_ end.

    Others have already about the need of the shebang line to run as a python script, but I'm
    surprised no one mentioned how truly useless this code is.

    The i = 0 line is totally unnecessary. The variable i is created and set to zero by the first
    iteration of the for loop. The break will abort the for loop (NOT the while loop) in the first
    iteration, and the 2nd through the 10th iterations will be skipped altogether.
    To be fair the range() call will be fairly time and memory-consuming on
    python 2, since it returns a list.
    This effectively leaves your code as:

    while True:
    pass # Do nothing, forever

    An empty loop as a time delay can occasionally be useful, but as a practical matter, a for loop
    with only 10 (empty/pass) iterations is probably too short for anything useful. Instead of
    being empty, do some more complex (but ignored) operation -- say math.sqrt() or math.sin() for
    example -- and a much larger repetition count. But it's likely that does use up processor
    cycles unnecessarily, although it can give you delays of fractions of seconds. If you want
    delays greater than a second, check out the time.sleep() function.

    -=- Larry -=-

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 17, '11 at 8:27a
activeFeb 18, '11 at 5:48p
posts13
users8
websitepython.org

People

Translate

site design / logo © 2022 Grokbase