[Tutor] Running .py files in shell

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

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

>> The simplest way is to put a "shebang" line at the top of your 
>> script
>> and then make them executable:
>> $ cat>  myscript.py
> What is the significance of this and how do I use it?

> >>> cat > tryme1.py

Notice the difference in the prompt. A dollar sign is the generic
way of indicating a Unix shell prompt.

cat is a Unix command which catenates the output to a file, again 
cat like this is a generic way of sayong create a file containing 
follows cat. In practice you would use a text editor like vim or pico 

means the python prompt. You are typing a Unix command into Python
which doesn't reciognise it, hence the error.

My apologies for not being explicit. I assumed since you were using
the shell to run sudo port install that you were an experienced Unix
shell user and would understand the significance of $.

(Incidentally % is the generic way of indicating a rioot user command,

% cat > mydfile

implies login as root (or use sudo) to type the command....)

>> Then use chmod +x to make it executable
>> $ chmod +x myscript.py
> When I try to run this with one of my files I get the following 
> error:
> >>> chmod +x tryme1.py

Same problem. chmod is the unix command to Change Mode of a file.

>> $ myscript.py
>> Alternatively you can just call python explicitly:
>> $ python myscript.py
> I get a syntax error doing this too:
> >>> python tryme1.py

And again, you type python on its own to start a Python interpreter 
You type, in Unix shell, python file.py to get Python to execute 

You can find a box explaining some of this in my tutor in the Style 
near the bottom...


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list