Using the Python interpreter

T

tkpmep

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:
Traceback (most recent call last):
File "<stdin>", line 1
python Script1.py
SyntaxError: invalid syntax

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

and if I edit it, I can then execute it by reloading it
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
 
S

Stevie

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:

Traceback (most recent call last):

File "<stdin>", line 1
python Script1.py
SyntaxError: invalid syntax

I can load it (and have it execute) by typing

0
1
2
3
4

and if I edit it, I can then execute it by reloading it

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
 
M

Michael Hoffman

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:

Traceback (most recent call last):

You need to do this from a Windows command line, not from Python.

C:\Python25>python Script1.py
Hello, world!
 
J

John Zenger

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:


Traceback (most recent call last):


File "<stdin>", line 1
python Script1.py
SyntaxError: invalid syntax

I can load it (and have it execute) by typing


0
1
2
3
4

and if I edit it, I can then execute it by reloading it


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:

See http://pyref.infogami.com/execfile
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top