# how to run *.py (Python) script



## jaschawrites (Mar 23, 2008)

help!  how do i run *.py (Python) scripts downloaded from a trusted source on the internet?  they currently open with TextEdit, displaying very readable code--but how do i go about running them?

am running OS 10.4.11 on an iBook G4, have not installed any special software to program in Python or to run Python scripts;  looking for simplest possible solution

thanx for any tips

jascha


----------



## Mikuro (Mar 23, 2008)

The most standard way to run them is to open Terminal (in /Applications/Utilities), then type "python " (note the space after "python"), then drag your .py file in the Finder onto the Terminal window. The resulting command should look something like this:

```
python /Users/yourname/Desktop/somescript.py
```
Hit Return to run the script.

That's not the only way, though. My favorite way is to use TextWrangler, a free text editor geared toward programming. You can edit Python scripts and run them right from within TextWrangler.


----------



## Giaguara (Mar 23, 2008)

http://developer.apple.com/internet/opensource/opensourcescripting.html
http://www.python.org/download/mac/ for a start// the ADC version above should do for basic python needs


----------



## basic (Oct 18, 2011)

Hello, I'm a student and just learning to write in python.. and the following was written in class by the instructor and executed and it worked fine.

#Name: *******
#Date: Oct.3, 2011
#Purpose: To ask for two numbers and display their sum

def main():
    real number1, number2

    print("This program takes two numbers from the user and adds them")
    #ask for first number
    number1 = float(input("First Number: "))
    number2 = float(input("Second Number: "))
    #call the function add_numbers
    add_numbers(number1+number2)


def addNumbers(n1,n2):
    #real n1, n2, sum_of_numbers

    sum_of_numbers = n1 + n2

    print ("------------------SUM OF NUMBERS---------------------")
    print ("First Number:%.2f" %n1)
    print ("Second Number:%.2f" %n2)
    print ("Sum:%.2f" %sum_of_numbers)
    print ("-----------------------------------------------------")
#--------------------------------------------------------------------
main ()

How ever if I executed the same script on Textwrangler I get an error reading "*.py has non-unix line breaks. #! script interpreters typically require unix breaks to work properly. Do you wish to run it anyway?"
So I continue anyway and I get a syntax error. (Though in class we were obviously using Python, not Textwrangler.)


----------



## Mikuro (Oct 19, 2011)

At the bottom of your TextWrangler window, there are several popup menus with options. One of them is for the line-break style. It probably says "Windows (CRLF)". Change it to "Unix (LF)" and save your file. Then it should work.

This problem stems from the fact that different OSes encode line breaks differently. The old Mac OS (before OS X) used ASCII character 13 ("carriage return", or "CR"). Unix (and by extension OS X) generally uses ASCII 10 ("line feed" or "LF"). Windows uses both, one after the other ("CRLF").

Unix scripts are generally unforgiving of alien line breaks. This extends to Python.


----------



## basic (Oct 19, 2011)

Mikuro said:


> At the bottom of your TextWrangler window, there are several popup menus with options. One of them is for the line-break style. It probably says "Windows (CRLF)". Change it to "Unix (LF)" and save your file. Then it should work.
> 
> This problem stems from the fact that different OSes encode line breaks differently. The old Mac OS (before OS X) used ASCII character 13 ("carriage return", or "CR"). Unix (and by extension OS X) generally uses ASCII 10 ("line feed" or "LF"). Windows uses both, one after the other ("CRLF").
> 
> Unix scripts are generally unforgiving of alien line breaks. This extends to Python.



Hello and thank you for the reply.
It turns out I have another problem in the script other than line-break style. This one is in line 6 _"real number1, number2"_. That apperently is where the syntax error is.


----------



## Mikuro (Oct 19, 2011)

Two things:

First, indentation is part of the syntax in Python. I can't tell if the lack of indentation is in your actual file or just showing up here because of copy-paste. Anyway, make sure the code after main() is indented.

Secondly, "real number1, number2" doesn't even look like Python. You do not declare variables in Python like you would in C. You just use them. It's dynamic. Perhaps this is an exercise in fixing broken code? Just from a quick glance I think there are other errors as well. If your teacher told you this was working code, he's probably being sneaky.

Good luck!


----------



## basic (Oct 20, 2011)

thanks. I'm dissapointed with my institute


----------

