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

6

76

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.

Palaeography answered 7/1, 2011 at 10:10 Comment(3)
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?Argentiferous
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 onePalaeography
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.Trabzon
B
52

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

Blevins answered 7/1, 2011 at 10:22 Comment(2)
Thx. I think this works for me and has the minimum configuarion boilerplatePalaeography
to my people (the lazies), "this" link takes you to: exec(open("./filename").read())Holpen
C
46

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)

Cantatrice answered 7/1, 2011 at 10:12 Comment(1)
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!Impacted
I
14

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

Irmairme answered 7/1, 2011 at 11:7 Comment(2)
This doesn't work for me. I can't access any variables. Could it be that I put my script in main()?Sharper
yes, the local variables in functions will be out of scope.Berner
P
5

You can also use the subprocess module. Something like:

>>> import subprocess
>>> proc = subprocess.Popen(['./script.py'])
>>> proc.communicate()
Prize answered 7/1, 2011 at 10:17 Comment(0)
C
2

You can run any system command using python:

>>>from subprocess import Popen
>>>Popen("python myscript.py", shell=True)
Canadianism answered 7/1, 2011 at 10:15 Comment(1)
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.Irmairme
A
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.

Ammon answered 1/9, 2018 at 9:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.