I'm a Python, and I'm a Mac.
July 15, 2009 6:41 PM   Subscribe

Help me learn Python! On a Mac!

I'd like to start getting to really know a programming language, and I've determined Python to be the best choice (it seems to be useful and able to make things and fairly intuitive—if I'm wrong, tell me!). I've got vague backgrounds in Java, a tiny bit of C, and web design.

I'd prefer to avoid the "Learn something else before Python!" speech unless you have a really good reason.

Also, I'm on a Mac. How do I write/compile/etc. in Python? Terminal? Xcode? Something else that's really cool and I'm unaware of?

Thanks so much!
posted by reductiondesign to Computers & Internet (19 answers total) 39 users marked this as a favorite
Response by poster: Also, any resources on getting started would be awesome. Not in original question due to previous questions that I've found. (And I forgot.)
posted by reductiondesign at 6:43 PM on July 15, 2009

Best answer: I did this. First, Python is a scripting language, so there's no compiling. Second, check out Dive into Python (free), and I also really like Learning Python (not free). You can start the python interactive prompt in the terminal by typing in:


Simple? Yes. The interactive prompt lets you enter Python scripts line-by-line, which is great for learning the language. Once you move beyond the learning/testing stages, scripts are just run by saving to a file using your favorite text editor (I'm a Textmate guy, myself) and running:

python foo.py

You can also install IDLE via MacPython. In fact, that last link is a pretty good one-stop-shop for Python-on-Mac. I can't stand the IDLE font, but IDLE is otherwise pretty great.
posted by The Michael The at 6:51 PM on July 15, 2009 [1 favorite]

Also, know that there are some pretty significant differences between Python 2.x and 3.x, as laid out here, so be aware of the version you're using on your Mac and what version is being taught in a given reference (e.g. I don't know if Learning Python has a 3.x version yet) to avoid conflicts in your learning.

In any case, make good use of the documentation.

I really enjoyed the process, and I think Python is the cat's pajamas (the snake's cake?). It's a great OOP language to learn.
posted by The Michael The at 7:02 PM on July 15, 2009

Best answer: Learning to program Python on a Mac is no different than learning on any other unix. It's even already installed--unless you want Python 3k.

Python is not compiled*. Python is interpreted*. This means that the actual text file that you edit is used as the program itself*. So, with C or Java, you write the code, then compile it to a binary format, and then the binary format is run as the program. With python, the source code is the executable.

So, to run some python code, assuming it's in a file called "myprogram.py" in the current directory, you simply type "python ./myprogram.py" on the command line.

For editing code, I use emacs from the terminal. However, you can use just about any programmer's text editor--most of them know about python by now. Avoid XCode or any other IDE during learning--you'll spend more time trying to figure out how to add a file to your project than you will learning to code. You basically just want a text editor that does Python syntax highlighting and nothing else. The same tools that make my life easier as a professional programmer will make your life as a novice programmer much harder.

I assume that you've found the documentation. The tutorial is a good place to start. There are a zillion other tutorials on the web as well; plus an indeterminate number of books. I liked the O'Reilly book on learning python (I think it's called "Learning Python"; it's not "Programming Python", which is more advanced), but I have no idea how current it is.

Incidentally, you should also be learning the Unix command line as you go. Do not attempt to find graphical tools to "simplify" everything. My tutoring students who embrace the command line invariably learn to program faster than my students who spend all their time dicking around learning non-standard graphical tools. Likewise, the command line is everywhere... your weird non-standard freeware tool is not.

*Technically, Python is compiled just in time to a byte-code format. However, this step is invisible to the user. It's just as well, while learning, to ignore this technicality.
posted by Netzapper at 7:03 PM on July 15, 2009

Not to schill, but BBEdit (link) is a great elegant steroid-laden text editor and has syntax highlighting for Python. I highly recommend it especially if you continue to dabble in other web design and coding efforts- it makes a nice IDE for interpreted languages and has been an indispensable app for many over the years.
posted by oldefortran at 7:40 PM on July 15, 2009

Ok, I will assume you've never programmed on a Mac, and suggest something different from usual...

1. write in a file (called test.py) the following:
raw_input("press any key to end")

2a. double-click on that file in finder; if you see a window appearing with "press...", then you're all set; otherwise

2b. in Applications/Utilities, double click on Terminal.app ; then do "cd folder_where_test.py_is_located", followed by test.py to see the above-mentioned window appear.

3.Go to http://code.google.com/p/crunchy and download Crunchy.

4. Follow the instructions in the readme file to install it.

5. Use Crunchy with Firefox (which will get started by Crunchy - assuming it is your default browser) to "browse" (from the menu) to the official Python tutorial.

You will then be able to go through the official Python tutorial with Firefox as your browser, executing all the code samples (and modifiying them as needed) directly in your browser window.
posted by aroberge at 7:43 PM on July 15, 2009 [3 favorites]

Best answer: Nothing beats the Official Python Tutorial, especially if you have prior programming experience. How to Think Like a Computer Scientist: Learning with Python is helpful as well (if a little dry). The voidspace Python OOP tutorial effectively introduces object-oriented programming in Python.

If you're using OS X, I highly recommend learning how to program Python from the command line first. Most of the other Python programming you do will assume that you have that basic skill. Here's a good tutorial for learning the UNIX command line (although it may require some adaptation for OS X specifically).

Now for a bit of self-linking: I recently made very brief Python tutorial for my graduate school colleagues, which (I'm told) some people have found helpful as a springboard. I'm also currently teaching an introductory Python course (though with a kind of strange creative writing focus). All the notes are online, and you're welcome to follow along.
posted by aparrish at 7:51 PM on July 15, 2009 [2 favorites]

RUR-PLE was designed to teach Python to schoolkids. It's fun to use and there's a DMG file for OSX.
posted by jquinby at 7:51 PM on July 15, 2009

To totally shill, learn vi because GUI text editors are for the weak minded. vi on the mac should have syntax highlighting for python. if not, install vim.
posted by GuyZero at 7:54 PM on July 15, 2009

Best answer: I advise sticking with Python 2.4-2.6... the 3.x series isn't well supported enough yet and the tutorials you find will almost certainly be based on 2.x.

The easy way to tell the difference in code examples between 2.x and 3.x is that 3.x has it's print statements as print("blah") with brackets whereas 2.x just has print "blah".
posted by holloway at 8:09 PM on July 15, 2009

If you do want Python 3 though then there's a special diveintopython3.org book being written.
posted by holloway at 8:10 PM on July 15, 2009

To totally shill, learn vi because GUI text editors are for the weak minded. vi on the mac should have syntax highlighting for python. if not, install vim.

How is this a shill? And how is vi with syntax highlighting any different from a decent syntax highlighting editor like BB Edit or Komodo, except for having to deal with a confusing mode switch?
posted by fatbird at 8:30 PM on July 15, 2009

Best answer: Seconding "How to think like a computer scientist", recommended by aparrish above. Also, once you get a little comfortable with Python, check out the Python challenge. I'm working (slowly!) through it now and already learned a ton from just the first half-dozen levels.

As for tools, I waffle between the free-and-barebones GUI text editor TextWrangler and vim, and the Terminal, and haven't needed much else. Crunchy sounds neat though!!
posted by estherbester at 9:42 PM on July 15, 2009

Some people will disagree, but if you are planning on doing any web development, get yourself Django (a python web framework) and work through the tutorials. The documentation is quite good, and it gives you enough of a framework that you can focus your learning of python on implementing your cool idea for a little web app, rather than on the important but frustrating infrastructure, plus, lots of source to look at and learn from.

Nuts to those who think you need to start close to the bare metal. If you want to do something that requires digging deeper, you'll do it.
posted by Good Brain at 11:46 PM on July 15, 2009

Best answer: A few points:

1. Learn and use vim. The version that comes with the system should be fine to start. The best way to learn to use vim is to open Terminal and type 'vimtutor' (without the quotes). The key is to get the basics down (switching between modes, using the keys on the home row to move around) and then to try and keep learning things, a little bit at a time. Macvim is also really nice. There are a bunch of blog posts and such out there on setting up vim as a development environment for python. Also, you could learn and use that... other well-known unix text editor, but everyone knows it's inferior (/me ducks).

2. The resources and books posted here are good, but I've found that one of the best ways to really learn a language/framework is to do something with it. So, think of something that you want to program and write it in python. Could be a website (django/pylons), a game (pygame), or anything else. Having something to work on will give you a reason to learn more and explore the language.

3. If you have questions, stackoverflow.com can be a decent place to look/ask for an answer. It's not quite as good as it was when it first started, but you can still get most questions answered there pretty fast and often, someone has already asked the same thing.
posted by pwicks at 11:48 PM on July 15, 2009

There are three more links to stuff designed for kids learning Python in the Scratch thread on the blue. I'm going this route using IDLE on my Mac and it's working fine for me. (I hate vi, and anyway am choosing not to fuss about which editor is my very favorite for now. For what it's worth, my developer pal told me about SciTE, which also seems a fine choice.)
posted by clavicle at 10:02 AM on July 16, 2009

No one has mentioned Nodebox yet. Python is a perfect first language, and a Mac is great to learn it on.
posted by doteatop at 10:48 AM on July 16, 2009

Just about any text editor other than TextEdit will do. My choices these days run to TextMate if you wan to spend money on a mac-centric editor, and TextWrangler if you want something that's free. EMACS and VIM are both really great editors if you want to go that direction.

I'll also suggest iTerm because you will be testing and running things at the command line a lot. iPython is also worth getting because it adds many of the interactive shell features of bash and zsh including autocompletion, and better command-line memory.
posted by KirkJobSluder at 12:45 PM on July 16, 2009

I don't really play around with Mac much, so all of my experiences with Python are limited to Windows and Linux. That said, while there are many great links above for learning Python, I have found that during the learning process, two commands in particular have really come in handy: 'help' and 'dir'. I find these commands just as handy as the 'man' command on Linux. As mentioned above, the interactive shell is a great way to play around, whether it is the default shell, iPython, IDLE, etc.

Regarding 'compiling', it's been mentioned above that there is no compile step. However, if you want to make applications that don't require the user to have Python installed (and the appropriate libraries), there are setuptools libaries available for that: I use py2exe for Windows, and I understand py2app is a similar library, but for Mac.

You should have a lot of fun with Python - this language really revived my love of programming.
posted by mysterpigg at 8:08 PM on July 22, 2009

« Older how many pages in a website?   |   Fashion Sense Me! Stat! Newer »
This thread is closed to new comments.