Search the web
Sign In
New User? Sign Up
PhillyPug · Python Users Group
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Real people. Real stories. See how Yahoo! Groups impacts members worldwide.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Two beginner questions about paths and running programs in Python:   Message List  
Reply | Forward Message #321 of 364 |
I am running Python 2.6.2 on my windows 7 RC 1 x64 laptop and attempting to
learn python as a beginner programmer on my own, but I'm having difficulty
getting python to run the .py files I create through the IDLE shell and from the
command prompt using the commands from the book located at
http://www.openbookproject.net/thinkcs/python/english2e/index.xhtml

I'd like to be able to call files within python programs and it seems that
whenever I list the path to a folder on my computer - I only get errors:

the book tells me that to call a python program you have to type

"$ python firstprogram.py"

This doesn't work on my computer from within idle, or from the command line...
I'm obviously doing something wrong, but maybe the book could be made to clarify
this?

For example - I'm trying to call the program I wrote - madlib.py, stored at
(C:\Users\Shawn\Documents\Pydoc\madlib.py)

I tried in multiple ways. I can only run files using the idle file menu or the
windows file system point and click way... Maybe the book could be more clear
for beginners like myself in this particular section, because I would like to
delve into learning to program in python... but I can't get the files I'm
writing to run properly:

Here is a subset of my various attempts separated by blank lines

>>> $ python madlib.py
SyntaxError: invalid syntax

>>> madlib.py
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
madlib.py
NameError: name 'madlib' is not defined

>>> python madlib.py
SyntaxError: invalid syntax

>>> C:\Users\Shawn\Documents\Pydoc\madlib.py
SyntaxError: invalid syntax

>>> python C:\Users\Shawn\Documents\Pydoc\madlib.py
SyntaxError: invalid syntax

>>> $ python C:\Users\Shawn\Documents\Pydoc\madlib.py
SyntaxError: invalid syntax

>>> import madlib.py

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import madlib.py
ImportError: No module named madlib.py

I then navigate using the IDLE "file - open " command and when choosing the file
the IDLE blinks and it runs the program showing...

>>> ================================ RESTART ================================
>>>
And then runs the program.

If I then type

>>> import madlib.py
It runs the program.

If I type it again…

>>> import madlib.py
I get the traceback error.

I know these are very simple questions - but in order to get into the nuances of
programming itself - I have to get past these system level errors… and be able
to manipulate the files I create.

Question 2: How does one create/add a default directory to python? I don't
seem to have permissions to write files to my python folder on windows 7, which
is another matter….

For instance I'd like to save a program I'm using called madlib.py to
"C:\Users\Shawn\Documents\Pydocs" and be able to access it through the command
line or through Idle without having to type the path…





Tue Jun 9, 2009 8:14 pm

shawnmoffit
Offline Offline
Send Email Send Email

Forward
Message #321 of 364 |
Expand Messages Author Sort by Date

I am running Python 2.6.2 on my windows 7 RC 1 x64 laptop and attempting to learn python as a beginner programmer on my own, but I'm having difficulty...
Shawn Moffit
shawnmoffit
Offline Send Email
Jun 9, 2009
8:14 pm

So, I've never used IDLE or Windows. That said, I think you are getting confused between the Python shell (which uses ">>>" as the prompt, and probably runs...
Erik Osheim
erik@...
Send Email
Jun 9, 2009
8:24 pm

so - once I get to the python command prompt, I should be able to open .py files - right? and am I doing that incorrectly or is that, from within python you ...
Shawn Moffit
shawnmoffit
Offline Send Email
Jun 9, 2009
9:05 pm

... Well, from python, you can import python modules (to import foo.py, you'd write "import foo"). You can't run python scripts from within python--or at...
Erik Osheim
erik@...
Send Email
Jun 9, 2009
9:52 pm

... open ... the ... terminal, ... not ... I'm...
Shawn Moffit
shawnmoffit
Offline Send Email
Jun 10, 2009
12:53 am

I know I'm not getting how the relative directory structure from python works... If I'm using python at c:\python26 and my module files are at...
Shawn Moffit
shawnmoffit
Offline Send Email
Jun 10, 2009
3:43 am

... In general, if you type "import foo" then either there should be a "foo.py" installed in one of the "system locations" (on linux this might be...
Erik Osheim
erik@...
Send Email
Jun 10, 2009
4:01 am

If that still doesn't work then you likely need to add python to windows path. For Windows 7 right-click on the "Computer" (aka My Computer) and choose...
Dave Teske
teskepython
Offline Send Email
Jun 9, 2009
9:20 pm

Thanks Dave - I made the changes and now it works from the command line as well. Very helpful....
Shawn Moffit
shawnmoffit
Offline Send Email
Jun 10, 2009
2:12 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help