is it possble to run a script from the python shell?

Discussion in 'Python' started by Darren Dale, May 4, 2004.

  1. Darren Dale

    Darren Dale Guest

    If so, how is it done? This is probably a stupid question. But when I
    os.chdir into a directory that I know contains a python file, when I try
    to call it

    >>>color_demo.py


    I get

    NameError: name color_demo is not defined

    Thanks,
    Darren
    Darren Dale, May 4, 2004
    #1
    1. Advertising

  2. Darren Dale

    Aahz Guest

    In article <>,
    Darren Dale <> wrote:
    >
    >If so, how is it done? This is probably a stupid question. But when I
    >os.chdir into a directory that I know contains a python file, when I try
    >to call it
    >
    >>>>color_demo.py

    >
    >I get
    >
    >NameError: name color_demo is not defined


    Try

    >>> execfile('color_demo.py')

    --
    Aahz () <*> http://www.pythoncraft.com/

    Adopt A Process -- stop killing all your children!
    Aahz, May 4, 2004
    #2
    1. Advertising

  3. Darren Dale

    Peter Hansen Guest

    Darren Dale wrote:

    > If so, how is it done? This is probably a stupid question. But when I
    > os.chdir into a directory that I know contains a python file, when I try
    > to call it
    >
    >>>>color_demo.py


    As Aahz said, use execfile(). I wanted to note, however, that
    calling the Python interactive interpreter prompt a "shell" is
    probably a poor idea, and a possible source of confusion for
    you. I believe "shells" will generally pass unrecognized commands
    on to another layer for further processing (and thus the name).

    Python's prompt is not a shell in that sense, as it recognizes only
    Python syntax and will give an error for any other commands. Typing
    just the name of a file, while it may work at your command line or
    Linux shell, is not valid Python syntax, so it doesn't work.

    There are some (or at least one) real shells for Python, in the
    sense of the word I think you're using. Look at
    http://ipython.scipy.org/ for further info about one such package.

    -Peter
    Peter Hansen, May 4, 2004
    #3
  4. | ....
    | >>> color_demo.py
    |
    | I get .... NameError: name color_demo is not defined
    | ....

    Darren ....

    At the Python prompt after the os.chdir
    you might also try ....

    import color_demo

    Or, directly from the command-line ....

    python /someDir/color_demo.py

    Or ....

    cd /someDir

    python color_demo.py

    --
    Cousin Stanley
    Human Being
    Phoenix, Arizona
    Cousin Stanley, May 4, 2004
    #4
    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. Replies:
    2
    Views:
    5,653
    Jonathan Bromley
    Feb 18, 2005
  2. Christian Heimes
    Replies:
    0
    Views:
    583
    Christian Heimes
    Feb 27, 2008
  3. Gerardo Herzig
    Replies:
    1
    Views:
    1,058
    Philipp Pagel
    Feb 27, 2008
  4. D'Arcy J.M. Cain
    Replies:
    0
    Views:
    843
    D'Arcy J.M. Cain
    Feb 27, 2008
  5. moongeegee

    execute a shell script in a shell script

    moongeegee, Dec 3, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    235
    Ben Morrow
    Dec 4, 2007
Loading...

Share This Page