Python on Mac

Discussion in 'Mac Programming' started by JD92, May 31, 2005.

  1. JD92 macrumors 6502a

    JD92

    Joined:
    Apr 14, 2005
    #1
    I've been trying learn Python on Windows for a week or two now and I just made the switch to Mac. Me and my friend like to mess about with stuff like this, and he uses Windows, so we'll be writing code on one platform and then doing it again on the other (he uses Windows).
    I've been hearing about how OS X has Python built in, but I can't find it anywhere. Anyone have any idea where to find the IDLE? I'm running OS 10.4.1
     
  2. Loge macrumors 68020

    Loge

    Joined:
    Jun 24, 2004
    Location:
    England
    #2
    You can access the built-in Python by going to a terminal window and typing "python". However, this Python does not include IDLE. For that you could download MacPython, which includes a package manager to make it easy to add further stuff like Numeric.

    Alternatively, if you want the latest Python it should be straightforward to compile from source.
     
  3. JD92 thread starter macrumors 6502a

    JD92

    Joined:
    Apr 14, 2005
    #3
    Thanks, I wrote a python program on the PC which ends in .py , how would you run a .py (or .pyw) file? It's pretty simple, doesn't use anything like Tkinter or anything fancy like that, just simple print, input and variables
     
  4. Omen88 macrumors regular

    Omen88

    Joined:
    Jan 8, 2002
    Location:
    Flanders (Belgium)
    #4
    in the terminal:
    python <nameofyourfile.py>

    Or am I missing something special you want to do?
     
  5. Loge macrumors 68020

    Loge

    Joined:
    Jun 24, 2004
    Location:
    England
    #5
    You can run a python script from Terminal, using syntax like,

    python ~/Documents/first.py

    if, say, you had a python script called first.py in your home Documents folder.

    Good luck with Python, btw. It's a cool language though I don't have the free time to explore it more at present.
     
  6. JD92 thread starter macrumors 6502a

    JD92

    Joined:
    Apr 14, 2005
    #6
    No, I'm just totally new to this. Do you have to just put 'xyz.py' or something like 'iMac/Documents/Python.xyz.py'

    EDIT: It's OK, I've figured it out
     
  7. superbovine macrumors 68030

    superbovine

    Joined:
    Nov 7, 2003
  8. WebMongol macrumors member

    Joined:
    Sep 19, 2004
    Location:
    Bay Area, CA
    #8
    Make Python script executable

    You can make Python script executable and run it by typing file name.
    To make python script executable you need:

    - add following first line to your python file:
    #! /usr/bin/python
    - make you python file executable:
    $ chmod +x first.py

    Example:

    flat:~/$ cat ./first.py
    #! /usr/bin/python
    print "Hello World!"
    flat:~/$
    flat:~/$ python first.py
    Hello World!
    flat:~/$
    flat:~/$ chmod +x first.py
    flat:~/$ ls -l first.py
    -rwxr-xr-x 1 webmongol staff 40 May 31 22:46 first.py
    flat:~/$./first.py
    Hello World!
    flat:~/$
     
  9. Loge macrumors 68020

    Loge

    Joined:
    Jun 24, 2004
    Location:
    England
    #9
    Good advice, though I think the last three lines above should be -

    flat:~/$ ./first.py
    Hello World!
    flat:~/$

    That is you can now run your script without calling python on the command line.
     
  10. WebMongol macrumors member

    Joined:
    Sep 19, 2004
    Location:
    Bay Area, CA
    #10
    Thanks for correction.
     

Share This Page