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

Peter Hansen peter at engcorp.com
Tue May 4 08:37:45 EDT 2004


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



More information about the Python-list mailing list