pyhton newbie questions

having trouble running a python script.
i created some simple python scripts. and i can't seem to get them to run?
one file i called spam.py and i will list it's contents here.
[code]
print 2 ** 8
print 'the bright side ' + 'of life'
[code]

i stored the file in a directory in my home directory which i called python.
/users/jamescollins/python/spam.py

a book i am reading says
"once you've saved this text file, you can ask Python to run it by listing it's full filename as the first argument to a python command, typed at the shell prompt"
the output from the book after running the script is as follows:

[code]
% python spam.py
256
the bright side of life
[code]

in terminal i started Python
and then typed

[code]
python spam.py
[code]
i got the following error messages


[code]
python spam.py

File "<stdin>", line 1
python spam.py
^
SyntaxError: invalid syntax
[code]

just trying to run simple python scripts
any help would be appreciated.

one thing i did was go into terminal into my python directory, and typed
python spam.py
and the program ran in terminal
i think the book was telling me to run spam.py as an argument when i launched python?
how do i get the program to run in the interactive prompt?
i also used the import command to import a file which i called spam.py
here is the output from my terminal session

[code]
import spam.py

256
the bright side of life
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named py
[code]

does anyone know what this added text means?

[code]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named py
[code]
another thing i will add is that i have two versions of python on my computer.
one i think is python that came with the developer tools, which i run from terminal.
another is a version i downloaded from the internet.
the one i downloaded from the internet has text in different colors.
the one i run from terminal seems easier to open files because i can switch to the directory where i have my scripts, and then start python and run the scripts by importing them. i also can call up python with a file as an argument.
the one i downloaded from the internet is giving me error messages
like when i try to import spam.py i get the following

[code]
import spam.py


Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
import spam.py
ImportError: No module named spam.py
[code]
how do i import a file?
do i need to give it a path?

again any help would be appreciated.

Posted on Apr 29, 2008 12:07 PM

Reply
5 replies

May 2, 2008 10:44 AM in response to jamesapp

does anyone know how to set the PYTHONPATH env variable.

i opened .bash_profile from terminal
it was a hidden file.
i will list it's contents here:


# Setting PATH for MacPython 2.5
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/Current/bin:${PATH}"
export PATH


i was told to add the following line to my .bash_profile file:


$ export PYTHONPATH=.:$HOME/python:$PYTHONPATH


i tried adding above line a couple of different ways ie.
i added the line exactly before export PATH
i also tried replacing Home with /users/jamescollins...
and i still get error messages when i try to import scripts from a directory
on my computer.
the scripts are in /users/jamescollins/python
any help would be appreciated.

May 5, 2008 2:16 PM in response to jamesapp

What version of OSX are you on?

Leopard includes python 2.5.1. If you leopard you don't have to do anything. Already set up for you. if you're on another version, then you need to include the path in your path (for bash). that file is in your home directory. It's hidden.

Firs thing you should do is get a good text editor. Take a look at TextMate ( http://macromates.com/).

To run any python program type: python program.py

What book are you reading?

Oh one last thing, to find python, open a terminal window and type: whereis python. That should give you a path.

May 12, 2008 11:28 AM in response to jamesapp

i am running leopard 10.5.2
the python that came with the developer tools is macPython 2.5 it is located in /Developer/Applications/Utilities/macPython 2.5
the version i downloaded, which is the one i prefer, is in my dock and is located in
/Applications/macPython 2.5
i am reading Learning Python by Mark Lutz
i typed whereis python in terminal got back

/usr/bin/python

i don't know if this is the python that came with the developer tools or the one i downloaded.
i like the version of python that i downloaded it opens it's own IDLE with it's own prompt.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

pyhton newbie questions

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.