Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to run python-script from the python promt? [absolute newbie]

Reply
Thread Tools

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

 
 
nukeymusic
Guest
Posts: n/a
 
      12-18-2011
How can I load a python-script after starting python in the
interactive mode?
I tried with
>>>load 'myscript.py'
>>>myscript.py
>>>myscript


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
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      12-18-2011
On Sun, Dec 18, 2011 at 10:00 PM, nukeymusic <(E-Mail Removed)> wrote:
> How can I load a python-script after starting python in the
> interactive mode?
> I tried with
>>>>load 'myscript.py'
>>>>myscript.py
>>>>myscript

>
> 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
 
Reply With Quote
 
 
 
 
Kev Dwyer
Guest
Posts: n/a
 
      12-18-2011
nukeymusic wrote:

> How can I load a python-script after starting python in the
> interactive mode?
> I tried with
>>>>load 'myscript.py'
>>>>myscript.py
>>>>myscript

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

 
Reply With Quote
 
Lie Ryan
Guest
Posts: n/a
 
      12-18-2011
On 12/18/2011 10:00 PM, nukeymusic wrote:
> How can I load a python-script after starting python in the
> interactive mode?
> I tried with
>>>> load 'myscript.py'
>>>> myscript.py
>>>> myscript

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

 
Reply With Quote
 
nukeymusic
Guest
Posts: n/a
 
      12-18-2011
On 18 dec, 13:39, Lie Ryan <(E-Mail Removed)> wrote:
> On 12/18/2011 10:00 PM, nukeymusic wrote:
>
>
>
>
>
>
>
>
>
> > How can I load a python-script after starting python in the
> > interactive mode?
> > I tried with
> >>>> load 'myscript.py'
> >>>> myscript.py
> >>>> myscript

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


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

is this correct?

thanks
nukey
 
Reply With Quote
 
Lie Ryan
Guest
Posts: n/a
 
      12-19-2011
On 12/19/2011 12:16 AM, nukeymusic wrote:
> On 18 dec, 13:39, Lie Ryan<(E-Mail Removed)> wrote:
>> On 12/18/2011 10:00 PM, nukeymusic wrote:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>> How can I load a python-script after starting python in the
>>> interactive mode?
>>> I tried with
>>>>>> load 'myscript.py'
>>>>>> myscript.py
>>>>>> myscript

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

>
> I guess you mean the following command?
> %run 'myscript.py'
>
> is this correct?


yes


 
Reply With Quote
 
Admin
Guest
Posts: n/a
 
      12-20-2011
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
>>>> load 'myscript.py'
>>>> myscript.py
>>>> myscript

>
> 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"
 
Reply With Quote
 
kimma
Guest
Posts: n/a
 
      12-20-2011
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
>>>> load 'myscript.py'
>>>> myscript.py
>>>> myscript

>
> 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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Run a python script as an exe and run a new process from it venutaurus539@gmail.com Python 9 02-27-2009 05:22 PM
EMBEDDING > Run Python & Run C Function anonymisiert85@mymail.ch Python 3 06-23-2007 04:15 AM
Can't Run 'RUN' Windows XP Home Luke O'Malley Computer Support 2 09-01-2005 11:38 PM
can run javascript can't run vbscript - WHY duncan ASP .Net 2 10-27-2004 09:31 AM



Advertisments