Is there a possibility to execute a Python script while being in interactive mode

Solution 1:

Use execfile('script.py') but it only work on python 2.x, if you are using 3.0 try this

Solution 2:

import file without the .py extension will do it, however __name__ will not be "__main__" so if the script does any checks to see if it's being run interactively you'll need to bypass them.

Alternately, if you're wanting to have a look at the environment after the script runs try python -i script.py

EDIT: To load it again

file = reload(file)

Solution 3:

You might want to look into IPython, a more powerful interactive shell. It has various "magic" commands including %run script.py (which, of course, runs the script and leaves any variables it defined for you to examine).

Solution 4:

You can also use the subprocess module. Something like:

>>> import subprocess
>>> proc = subprocess.Popen(['./script.py'])
>>> proc.communicate()

Solution 5:

You can run any system command using python:

>>>from subprocess import Popen
>>>Popen("python myscript.py", shell=True)