user2779936
user2779936

Reputation: 23

How do I run a scipt from within Python on windows/dos?

All I know how to do is type "python foo.py" in dos; the program runs but then exits python back to dos. Is there a way to run foo.py from within python? Or to stay in python after running? I want to do this to help debug, so that I may look at variables used in foo.py (Thanks from a newbie)

Upvotes: 2

Views: 88

Answers (3)

Joran Beasley
Joran Beasley

Reputation: 114038

add the module q , and use its q.d() method (I did it with easy_install q) https://pypi.python.org/pypi/q

import q
....
#a bunch of code in foo.py
...
q.d()

that will give you a console at any point in your program where you put it that you can interact with your script

consider the following foo.py

import q
for x in range(5):
    q.d()

now examine what happens when I run it

C:\Python_Examples>python qtest.py
Python console opened by q.d() in <module>
>>> print x
0
>>>
Python console opened by q.d() in <module>
>>> print x
1
>>>
Python console opened by q.d() in <module>
>>> print x
2
>>>
Python console opened by q.d() in <module>
>>> print x
3

(note to continue execution of script use ctrl+z)

in my experience this has been very helpful since you often want to pause and examine stuff mid execution (not at the end)

Upvotes: 1

Parzival
Parzival

Reputation: 2064

To stay in Python afterwards you could just type 'python' on the command prompt, then run your code from inside python. That way you'll be able to manipulate the objects (lists, dictionaries, etc) as you wish.

Upvotes: 1

ChrisProsser
ChrisProsser

Reputation: 13098

You can enter the python interpreter by just typing Python. Then if you run:

execfile('foo.py')

This will run the program and keep the interpreter open. More details here.

Upvotes: 5

Related Questions