79

Normally you can execute a Python script for example: python myscript.py, but if you are in the interactive mode, how is it possible to execute a Python script on the filesystem?

>>> exec(File) ???

It should be possible to execute the script more than one time.

3
  • 2
    Why doesn't myscript.py have a proper "main" function? Why can't you import myscript and myscript.main()? That's the usual approach. Why won't that work? Can you fix myscript to add a proper "main" function? Commented Jan 7, 2011 at 12:05
  • The problem is that I'm loading an "paster shell" and while doing this other project libs are loaded. And in this interactive shell I want to try some things out. But yes you are right your suggestion is a good one Commented Jan 7, 2011 at 19:53
  • 6
    This doesn't answer the question as you've asked it, but in case it's relevant to you or others, I find this useful when I'm in active development: PYTHONSTARTUP=some_script.py python -i. This will execute some_script.py and drop you to an interactive shell. If your working script defines local variables, you'll be able to access them from the shell. This can be really handy when trying to experiment with code or analyze behavior after the fact. Commented Jun 13, 2014 at 18:20

6 Answers 6

55

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thx. I think this works for me and has the minimum configuarion boilerplate
to my people (the lazies), "this" link takes you to: exec(open("./filename").read())
48

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)

1 Comment

Alternately, if you're wanting to have a look at the environment after the script runs try python -i script.py This is epic thank you!
15

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).

2 Comments

This doesn't work for me. I can't access any variables. Could it be that I put my script in main()?
yes, the local variables in functions will be out of scope.
5

You can also use the subprocess module. Something like:

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

Comments

2

You can run any system command using python:

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

1 Comment

Subprocess isn't ideal for this. You won't be able to interact with its variables, because it's in an entirely separate process, and you need another command even to see its output.
2

The easiest way to do it is to use the os module:

import os

os.system('python script.py')

In fact os.system('cmd') to run shell commands. Hope it will be enough.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.