I have a simple python script (say, simple.py) , like
a = 5
b = a + a
print(b)
But I want the following: output the result as if these commands were executed in the interpreter, like
>>> a = 5
>>> b = a + a
>>> print(b)
10
And I want this in stdout 🙂
Is that possible in some simple way?
Also the same question for IPython.
This question can be helpful for writing a small “how-to” for working with Python/IPython interpreters
You can use the
execstatement to execute a line of code (the following is Python 2 code, change the print statements toprint()if you want it to be Python 3):I should note that it does not appear to handle multi-line statements very well yet (as it’s evaluating each line separately).