Python is one of the most popular programming languages. Students use Python to learn coding; data scientists use Python to crunch numbers. Discover how to set up and learn Python coding on a Mac, including the best Python text editors and the best Python training courses
I want to learn coding on my Mac – how do I set up Python, and where’s the best place to learn Python coding?
Learning to code is hugely popular at the moment, and Python is a great coding language to learn. Luckily for us, the Mac is a great coding platform, and Python makes it easy to learn how to code on a Mac.
In this feature, we’re going to look at setting up Python in OS X. It’s very easy to explore Python on a Mac, and Python should be installed on your Mac already. You can test this out by opening Terminal and entering python –version. It should display Python 2.7.10 (you may have a slightly different version).
How to set up and learn Python coding on a Mac: Install the latest version of Python 2
Python comes installed along with OS X, although you can make sure you have the latest version of Python by installing the current build from the Python website.
- Download the Mac OS X 64-bit/32-bit installer (not the PPC installer) from the Python website.
- Double-click the python-2.7.11-macosx10.6.pkg file in the Downloads folder.
- If you have Gatekeeper enabled, the installation will be blocked. Open System Preferences Security Privacy and click Open Anyway.
- Click Continue, Agree and Install buttons in the Install Python window.
Open Terminal again and enter python –version. It should now read Python 2.7.11.
How to set up and learn Python coding on a Mac: Which version of Python should I use?
One confusing aspect of Python is that there are two major versions available, Python 2 and Python 3 (each with several sub-versions). What’s the difference between Python 2 and Python 3?
Python 3 is the newer version of Python, so you can be forgiven for assuming that it’s the best version. “Best” is pretty relative, though, and although Python 3 introduces a lot of new features, it also introduced a lot of incompatibilities.
Because coding in Python relies on using libraries of code developed by other people, this isn’t a good thing.
Also, we’ve found that the major learning programs, from MIT to Codecademy, all use Python 2 to explain coding. These programs won’t always work if you’re using Python 3. So until you know what you’re doing, stick with Python 2.
How to set up and learn Python coding on a Mac: Install Xcode and Command Line Tools
Now that you have Python installed you should install Xcode and the Command Line Tools. Command Line Tools install a GCC compiler, and a lot of Unix-based tools also need a GCC compiler, so this is the easiest way to get one. Although you won’t use Xcode to create Python programs, it’ll make life easier down the line. Follow these steps to install Xcode and the Command Line Tools.
- Open the App Store and search for Xcode.
- Click Get Install.
The Xcode Command Line Tools are installed alongside Xcode. Test that they’re installed by entering xcode-select -p. You should see /Library/Developer/CommandLineTools.
How to set up and learn Python coding on a Mac: Install Homebrew
Now that we’ve got Python and the Command Line Tools up and running, we should get Homebrew installed. Homebrew is a program used to install packages in OS X (it’s the equivalent of apt-get, a command common on Unix and Linux computers).
- Open a Terminal window
- Cut and paste this command: /usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”
When it has finished installing, enter brew –version to check it’s installed. And brew home to open the Homebrew webpage and learn how to use Homebrew.
Packages are installed using brew install. Unlike apt-get you don’t need to use sudo with brew. (Don’t worry if that previous sentence doesn’t make sense! You’ll get the hang of it as you start to learn Python.)
How to set up and learn Python coding on a Mac: Install PIP in OS X
PIP is a program used to install Python packages (these are pre-built blocks of code you’ll be using inside your programs). Installing PIP is easy. Open Terminal and enter this line: sudo easy_install pip. Enter pip –version to make sure it’s installed.
How to set up and learn Python coding on a Mac: The best Python text editors for Mac
By default, Python files (ending in .py) will open in TextEdit, and this won’t do. It has a nasty habit of a writing files in non-ASCII format, which messes things up. It also lacks any decent formatting options.
So you’ll need to install a great text editor. Fortunately, there are a few available. Here are the three best text-editing applications for Mac.
- TextWrangler: This app is a free download from the App Store. It’s a lightweight version of BBEdit but has all the functionality you need. The best choice for starting out.
- Geddit: This is a popular choice amongst Linux fans, so it’s good for programmers to learn. Not as slick as other options, but very functional.
- Sublime Text: This is my personal favourite. You can download and use it for free, although it’ll occasionally nag you into paying for it. A slick interface that’s easy on the eyes with great formatting control.
When you’ve installed your text editor, it’ll be important to associate it with Python files (ending in “py”.):
- Open the text editor and save a file called “test.py” to your desktop.
- Highlight the file and choose File Get Info (Command-I).
- Change the option under Open With to your program of choice.
- Click Change All.
How to set up and learn Python coding on a Mac: Create a “Hello, World!” program
Now that you’ve got Python installed correctly, let’s see how easy it is to create a Hello World program. “Hello, World!” is a standard first program that simply writes the words “Hello, World!” to the screen.
- Open Terminal.
- Enter touch helloworld.py to create a blank Python text file.
- Enter open helloworld.py. The file should open in the app you chose in the previous step.
- Enter a single line in the text file that reads: print “Hello, World!”
- Save the file (Command-S).
- Head back to Terminal and enter python helloworld.py.
Terminal should run the program and display “Hello, World!”
That is, at its most basic, how you create and run Python programs in a text edit app, and run them from the command line on a Mac. Of course, there’s much more to programming in Python that that. And there are lots of courses out there ready to help you.
How to set up and learn Python coding on a Mac: The best Python training courses for Mac
Once you’ve got Python installed on your Mac, you’ll want to start learning all the nuances of Python programming. Here are some of the best courses you can follow to learn Python programming.
- Codecademy Python Course. This is a great website, although it teaches you how to use Python in a web browser, so don’t forget to keep creating programs in your text edit app and running them for real in Terminal.
- Learn Python the Hard Way. Don’t be intimidated by the title. This course teaches you the nuts and bolts of programming.
- Green Tea Press: Think Python. This free book takes you through different aspects of Python.
- Udemy: Introduction to Python for Beginners. This is a paid-for course, although Udemy often has deals so keep your eye out. It’s a good video tutorial that takes you through aspects of Python.
This entry passed through the Full-Text RSS service – if this is your content and you’re reading it on someone else’s site, please read the FAQ at fivefilters.org/content-only/faq.php#publishers.