Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   is it possble to run a script from the python shell? (http://www.velocityreviews.com/forums/t330559-is-it-possble-to-run-a-script-from-the-python-shell.html)

Darren Dale 05-04-2004 03:28 AM

is it possble to run a script from the python shell?
 
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

Aahz 05-04-2004 04:05 AM

Re: is it possble to run a script from the python shell?
 
In article <MPG.1b00d7f67c9240d6989682@newsstand.cit.cornell. edu>,
Darren Dale <dd55@cornell.edu> 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 (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/

Adopt A Process -- stop killing all your children!

Peter Hansen 05-04-2004 12:37 PM

Re: is it possble to run a script from the python shell?
 
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

Cousin Stanley 05-04-2004 04:05 PM

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

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


All times are GMT. The time now is 01:53 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.