Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > is it possble to run a script from the python shell?

Reply
Thread Tools

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

 
 
Darren Dale
Guest
Posts: n/a
 
      05-04-2004
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
 
Reply With Quote
 
 
 
 
Aahz
Guest
Posts: n/a
 
      05-04-2004
In article <(E-Mail Removed). edu>,
Darren Dale <(E-Mail Removed)> 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 ((E-Mail Removed)) <*> http://www.pythoncraft.com/

Adopt A Process -- stop killing all your children!
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      05-04-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
 
Reply With Quote
 
Cousin Stanley
Guest
Posts: n/a
 
      05-04-2004

| ....
| >>> 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
 
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
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
how to do "load script; run script" in a loop in embedded python? lipingffeng@gmail.com Python 1 04-07-2008 06:00 AM
Error when python script run as cgi script sophie_newbie Python 3 10-22-2007 05:39 PM
run a script and supply commands from a python cgi script moishyyehuda@gmail.com Python 1 12-14-2006 05:16 PM
how do i run another script from my python script nephish@xit.net Python 5 10-28-2005 02:06 AM



Advertisments