how to run python-script from the python promt? [absolute newbie]

N

nukeymusic

How can I load a python-script after starting python in the
interactive mode?
I tried with
but none of these works, so the only way I could work further until
now was copy/paste line per line of my python-script to the
interactive mode prompt
I do know how to run the script non-interactively, but what I want to
do is adding lines to the code I have written thus far in interactive
mode.

thanks in advance
nukey
 
C

Chris Angelico

How can I load a python-script after starting python in the
interactive mode?
I tried with

but none of these works, so the only way I could work further until
now was copy/paste line per line of my python-script to the
interactive mode prompt

The easiest way to load a file is to import it. However, this is not
quite identical to loading the script into the current session. If
your script mainly defines functions, you can either:

import myscript

or

from myscript import *

and it'll do more or less what you expect; however, it will execute in
its own module context, so globals from your current session won't be
available to it.

Tip for pasting: Indent every line of your code at least one
space/tab, prefix it with "if True:", and then you can paste it all at
once.

ChrisA
 
K

Kev Dwyer

nukeymusic said:
How can I load a python-script after starting python in the
interactive mode?
I tried with

but none of these works, so the only way I could work further until
now was copy/paste line per line of my python-script to the
interactive mode prompt
I do know how to run the script non-interactively, but what I want to
do is adding lines to the code I have written thus far in interactive
mode.

thanks in advance
nukey

Hello,

You can make the code in your script available to the interpreter by typing

import myscript

(assuming that you are running the interpreter in the same directory that
contains myscript.py)

You can access functions, classes and other top-level objects in your script
by prefixing their names with "myscript" and a dot (".") e.g.
myscript.myfunc, myscript.MyClass, myscript.myvar

You can't really edit your script in the interpreter, but you can edit and
save in a text editor and then type

reload(myscript)

in the interpreter to refresh its version of the myscript code.

N.B. when you import/reload your script the interpreter will immediately
execute any code that is not enclosed in a function or class definition.

Cheers,

Kev
 
L

Lie Ryan

How can I load a python-script after starting python in the
interactive mode?
I tried with

but none of these works, so the only way I could work further until
now was copy/paste line per line of my python-script to the
interactive mode prompt
I do know how to run the script non-interactively, but what I want to
do is adding lines to the code I have written thus far in interactive
mode.

thanks in advance
nukey

The normal python shell doesn't directly support doing that, although
there are several workaround with (ab)using the 'import' statement, it
had several subtleties with how module are cached. Try the ipython
shell; in ipython you can load a file into the current interpreter
session using the %run magic command.
 
N

nukeymusic

The normal python shell doesn't directly support doing that, although
there are several workaround with (ab)using the 'import' statement, it
had several subtleties with how module are cached. Try the ipython
shell; in ipython you can load a file into the current interpreter
session using the %run magic command.

I guess you mean the following command?
%run 'myscript.py'

is this correct?

thanks
nukey
 
A

Admin

Am 18.12.2011 12:00, schrieb nukeymusic:
How can I load a python-script after starting python in the
interactive mode?
I tried with

but none of these works, so the only way I could work further until
now was copy/paste line per line of my python-script to the
interactive mode prompt
I do know how to run the script non-interactively, but what I want to
do is adding lines to the code I have written thus far in interactive
mode.

thanks in advance
nukey
just go to the folder there there script is located and then do
"scriptname.py"
 
K

kimma

Am 18.12.2011 12:00, schrieb nukeymusic:
How can I load a python-script after starting python in the
interactive mode?
I tried with

but none of these works, so the only way I could work further until
now was copy/paste line per line of my python-script to the
interactive mode prompt
I do know how to run the script non-interactively, but what I want to
do is adding lines to the code I have written thus far in interactive
mode.

thanks in advance
nukey

In console, do "python script.py" (after cd'ing into the right directory)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,961
Messages
2,570,130
Members
46,689
Latest member
liammiller

Latest Threads

Top