2

I want to run some python code i.e. example.py.
I don't want to enter code manually into Python shell since it slow:

x = 1
x
y = 2
y + x
y

And I'd like to achieve such output:

>>> x = 1
>>> x
1
>>> y = 2
>>> y + x
3
>>> y
2

It looks that is simple to answer, but very hard to find how to do it.

Since there was some questions this is not pattern to document "large code" that is for documenting/testing code snippets/examples!

12
  • 1
    Why not write your code as actual Python rather than relying on the shell's interaction? Commented Feb 15, 2012 at 10:23
  • Why not use print? or try ironpython. Also you could write it in the shell and then convert the shell text into a python file. Commented Feb 15, 2012 at 10:37
  • 1
    Why not use copy and paste? How much of this do you have to do? Are you writing a tutorial in Python? If so, you only have to do it once and you're done. Please explain the use case for this. Commented Feb 15, 2012 at 10:44
  • @Lattyware since such format is good present simple examples. Commented Feb 15, 2012 at 10:50
  • @robertking Not want print since to slow to prepare examples. Commented Feb 15, 2012 at 10:51

4 Answers 4

4

You can do that with the InteractiveConsole in the code module, and it support multilines:

import code

console = code.InteractiveConsole()

more_input = False
with open('example.py') as source:
    for line in source:
        if not more_input:
            print('>>> ' + line.rstrip())
        else:
            print('... ' + line.rstrip())

        more_input = console.push(line)

If you wonder how the more_input flag works, take a look at the doc:

InteractiveConsole.push(line):

[...] The return value is True if more input is required, False if the line was dealt with in some way (this is the same as runsource()).

I've test it on an example.py that looks like:

x = 1
x
y = 2
y + x

a = (
    1,
    2,
    3
    )
print(a)

and the result was:

>>> x = 1
>>> x
1
>>> y = 2
>>> y + x
3
>>> 
>>> a = (
...     1,
...     2,
...     3
...     )
>>> print(a)
(1, 2, 3)
Sign up to request clarification or add additional context in comments.

2 Comments

@Chameleon: Whould this mean that you're going to accept it? :)
Sure since it the best! - You could improve little code style - order flow as data comes (simplify readability) - rename flag to meaning (readability) ... :) I give some hours to give chance others.
2

You could try something like this:

f = open('example.py', 'r')

for line in f:
  print('>>> ' + line)
  try:
    print(eval(line))
  except SyntaxError:
    exec(line)

3 Comments

be careful of multiline strings but this will work for his code.
You really need to do this: global _; _= eval(line); if _ is not None: print( _ ) to get slightly closer to REPL behavior.
@robertking sure it not support multiline but it look that is the best solution.
0

Assuming it's in the current directory:

execfile('example.py')

See execfile() docs.

1 Comment

This does not emit the >>> ... lines.
-1

I would not know a way to do so. But you don't have to enter it manually - just do copy & paste...

1 Comment

Not works if code is multiline and slow if you doing many code updates - need to repeat C&P.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.