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

Discussion in 'Python' started by nukeymusic, Dec 18, 2011.

  1. nukeymusic

    nukeymusic Guest

    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
     
    nukeymusic, Dec 18, 2011
    #1
    1. Advertising

  2. On Sun, Dec 18, 2011 at 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


    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
     
    Chris Angelico, Dec 18, 2011
    #2
    1. Advertising

  3. nukeymusic

    Kev Dwyer Guest

    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
     
    Kev Dwyer, Dec 18, 2011
    #3
  4. nukeymusic

    Lie Ryan Guest

    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.
     
    Lie Ryan, Dec 18, 2011
    #4
  5. nukeymusic

    nukeymusic Guest

    On 18 dec, 13:39, Lie Ryan <> 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
     
    nukeymusic, Dec 18, 2011
    #5
  6. nukeymusic

    Lie Ryan Guest

    On 12/19/2011 12:16 AM, nukeymusic wrote:
    > On 18 dec, 13:39, Lie Ryan<> 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
     
    Lie Ryan, Dec 19, 2011
    #6
  7. nukeymusic

    Admin Guest

    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"
     
    Admin, Dec 20, 2011
    #7
  8. nukeymusic

    kimma Guest

    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)
     
    kimma, Dec 20, 2011
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patio87
    Replies:
    4
    Views:
    418
    Julie
    Aug 18, 2004
  2. shanti bhushan

    subprocess in Command promt+ webbrowser

    shanti bhushan, Jun 19, 2010, in forum: Python
    Replies:
    2
    Views:
    460
    shanti bhushan
    Jun 19, 2010
  3. shanti bhushan

    start and kill process by command promt

    shanti bhushan, Jun 21, 2010, in forum: Python
    Replies:
    2
    Views:
    774
    Giampaolo RodolĂ 
    Jun 21, 2010
  4. Shyam Prakash

    Hiding cmd promt window

    Shyam Prakash, Feb 3, 2005, in forum: ASP .Net Security
    Replies:
    2
    Views:
    180
    Shyam Prakash
    Feb 4, 2005
  5. Li Chen
    Replies:
    0
    Views:
    116
    Li Chen
    Nov 26, 2006
Loading...

Share This Page