Using the Python interpreter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tkpmep@hotmail.com

    #1

    Using the Python interpreter

    Instead of starting IDLE as I normally do, I started the Python
    interpreter and tried to run a program. I got a Python prompt (>>>),
    and then tried unsuccessfully to run a Python script named Script1.py
    that runs perfectly well in IDLE. Here's what I did:
    >>>Script1.py
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    NameError: name Script1 is not defined
    >>>python Script1.py
    File "<stdin>", line 1
    python Script1.py
    SyntaxError: invalid syntax

    I can load it (and have it execute) by typing
    >>>import Script1
    0
    1
    2
    3
    4

    and if I edit it, I can then execute it by reloading it
    >>>import Script1
    0
    1
    2
    3
    4
    <module 'Script1' from 'Script1.pyc'>

    But this seems contrived - is there no way to repeatedly run Script1
    from the interpreter without reloading it?

    Thomas Philips

  • Stevie

    #2
    Re: Using the Python interpreter

    On Thu, 19 Apr 2007 01:32:36 +0100, tkpmep@hotmail. com wrote
    (in article <1176942756.283 173.234750@n76g 2000hsh.googleg roups.com>):
    Instead of starting IDLE as I normally do, I started the Python
    interpreter and tried to run a program. I got a Python prompt (>>>),
    and then tried unsuccessfully to run a Python script named Script1.py
    that runs perfectly well in IDLE. Here's what I did:
    >
    >>>Script1.py
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    NameError: name Script1 is not defined
    >
    >>>python Script1.py
    File "<stdin>", line 1
    python Script1.py
    SyntaxError: invalid syntax
    >
    I can load it (and have it execute) by typing
    >
    >>>import Script1
    0
    1
    2
    3
    4
    >
    and if I edit it, I can then execute it by reloading it
    >
    >>>import Script1
    0
    1
    2
    3
    4
    <module 'Script1' from 'Script1.pyc'>
    >
    But this seems contrived - is there no way to repeatedly run Script1
    from the interpreter without reloading it?
    >
    Thomas Philips
    >
    Why dont you wrap the code into a procedure (a def statement) in your script1
    file import it using the from xxx import yyy statement. Then you can use it
    over and over.

    By the sounds of it your script is full of inline code that is executed when
    its imported and therefore only runs once. To get it to re-run you have to
    repeatedly import it. Your really should only need to do this under very
    specific circumstances.

    Steve

    Comment

    • Michael Hoffman

      #3
      Re: Using the Python interpreter

      tkpmep@hotmail. com wrote:
      Instead of starting IDLE as I normally do, I started the Python
      interpreter and tried to run a program. I got a Python prompt (>>>),
      and then tried unsuccessfully to run a Python script named Script1.py
      that runs perfectly well in IDLE. Here's what I did:
      >
      >>>Script1.py
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      NameError: name Script1 is not defined
      >
      >>>python Script1.py
      You need to do this from a Windows command line, not from Python.

      C:\Python25>pyt hon Script1.py
      Hello, world!
      --
      Michael Hoffman

      Comment

      • John Zenger

        #4
        Re: Using the Python interpreter

        On Apr 18, 8:32 pm, tkp...@hotmail. com wrote:
        Instead of starting IDLE as I normally do, I started the Python
        interpreter and tried to run a program. I got a Python prompt (>>>),
        and then tried unsuccessfully to run a Python script named Script1.py
        that runs perfectly well in IDLE. Here's what I did:
        >
        >>Script1.py
        >
        Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        NameError: name Script1 is not defined
        >
        >>python Script1.py
        >
        File "<stdin>", line 1
        python Script1.py
        SyntaxError: invalid syntax
        >
        I can load it (and have it execute) by typing
        >
        >>import Script1
        >
        0
        1
        2
        3
        4
        >
        and if I edit it, I can then execute it by reloading it
        >
        >>import Script1
        >
        0
        1
        2
        3
        4
        <module 'Script1' from 'Script1.pyc'>
        >
        But this seems contrived - is there no way to repeatedly run Script1
        from the interpreter without reloading it?
        >
        Thomas Philips
        You want execfile:
        >>execfile("Scr ipt1.py")
        See http://pyref.infogami.com/execfile

        Comment

        Working...