61

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.

Delgan
  • 14,714
  • 6
  • 77
  • 119
lennykey
  • 983
  • 2
  • 10
  • 19
  • 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? – S.Lott Jan 07 '11 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 – lennykey Jan 07 '11 at 19:53
  • 5
    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. – Mark Jun 13 '14 at 18:20

6 Answers6

45

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

Delgan
  • 14,714
  • 6
  • 77
  • 119
fn.
  • 2,212
  • 1
  • 25
  • 25
32

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)

richo
  • 7,685
  • 1
  • 26
  • 47
  • 9
    `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! – Matthew Moisen Aug 25 '16 at 17:37
11

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

Thomas K
  • 35,785
  • 7
  • 76
  • 82
5

You can also use the subprocess module. Something like:

>>> import subprocess
>>> proc = subprocess.Popen(['./script.py'])
>>> proc.communicate()
Sylvain Defresne
  • 38,469
  • 11
  • 70
  • 82
2

You can run any system command using python:

>>>from subprocess import Popen
>>>Popen("python myscript.py", shell=True)
Silver Light
  • 37,827
  • 29
  • 116
  • 159
  • 3
    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. – Thomas K Jan 07 '11 at 11:12
1

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.

bohrax
  • 971
  • 7
  • 18
Cipher
  • 59
  • 6