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

D

Darren Dale

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

I get

NameError: name color_demo is not defined

Thanks,
Darren
 
A

Aahz

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


I get

NameError: name color_demo is not defined

Try
 
P

Peter Hansen

Darren said:
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

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
 
C

Cousin Stanley

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top