[Tutor] from command prompt use interactive python and running script together
eryksun
eryksun at gmail.com
Fri Feb 21 16:08:41 CET 2014
On Fri, Feb 21, 2014 at 9:20 AM, Gabriele Brambilla
<gb.gabrielebrambilla at gmail.com> wrote:
>
> Is possible on python to running scripts from the command prompt (I'm using
> python on windows) and in the end saving all the variables and continue the
> analysis in the interactive mode? (the one that you activate typing python
> in the command prompt?)
The -i option will inspect interactively after running a command,
module, or script:
python -i [-c cmd | -m mod | file]
Or using the new launcher that comes with 3.3:
py [-2 | -3 | -X.Y | -X.Y-32] -i [-c cmd | -m mod | file]
The 3.3 installer associates the new launcher with .py files. This
introduces Unix-style shebang support, e.g.:
#!python2 -i
A shebang lets you run the script directly in the console, or from the
Windows GUI shell. In other words, instead of running "python -i
script.py" you'd simply run "script.py", or just double-click on the
file icon in Explorer. Normally the console window that opens when you
run a .py file will automatically close when the script exits. But
with -i in the above shebang, the console stays open, with the
interpreter running in interactive mode.
> Or to use python in the interactive mode and in some moments to run scripts,
> without quit() the interactive mode, and use for the script variables the
> one you have defined in the interactive mode?
You can use runpy.run_path, which was added in 2.7/3.2. It returns a
dict namespace, and if you want you can simply update globals() with
it.
test.py:
def main():
print('spam')
if __name__ == '__main__':
main()
Demo:
>>> import runpy
>>> ns = runpy.run_path('test.py', run_name='__main__')
spam
>>> globals().update(ns)
>>> main()
spam
More information about the Tutor
mailing list