[Tutor] Running .py files in shell

Alan Gauld alan.gauld at btinternet.com
Wed Jul 28 01:01:19 CEST 2010


"Kevin Rapley" <kevin at digikev.co.uk> wrote

> I am running Mac OSX v10.6.4 Snow Leopard
> I am running Python 2.6.1

In general get the MacPython distributions of Python etc, they usually
install easier (ie from the GUI) than the "Unix" based versions.

> 1. How do I execute .py files in the command line shell? I have my 
> files in /Users/Kevin/python-exercises/ and am opening python in 
> shell from that directory

There are numerous ways, especially in MacOS.
The simplest way is to put a "shebang" line at the top of your script
and then make them executable:

$ cat > myscript.py
#! /bin/env python
# the above line must be the first line in your file and tells the
# shell where to find python
# rest of your code follows.....

Then use chmod +x to make it executable

$ chmod +x myscript.py

Now you can run it:

$ myscript.py

Alternatively you can just call python explicitly:

$ python myscript.py

You can do other things with file associations and
MacOS bundles/packages etc but unless its a major app
that you use a lot the techniques above are easier.

> 2. How do I install GASP which is referenced in chapter 4?

Can't help you there, sorry...

> I have MacPorts installed and have attempted to install PyObjC, 
> which is referenced as a prerequisite to PyGame and GASP. I get the 
> following error when attempting to install PyObjC:

Check the MacPython web site, I'm sure I used an install
package when I played with PyObjC...

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list