Here is a quick tour on how to setup an effective dev environment for Python 3 on your beloved OSX.
- Installing Homebrew
- Installing Python 3
- Installing Eclipse
- Installing PyDev for Eclipse
1. Installing Homebrew
There are many package installers on OSX but I find Homebrew very pleasant, simple and convenient. Homebrew, originally started by Max Howell, simplify the installation of open source tools (like ImageMagick, wget, ack etc..) that are not by default on OSX.
What I really like with Homebrew is that everything is installed on a directory that is not conflicting with the system directories. By default, it’s on
/usr/local but you can change to whatever you like.
Installing Homebrew is very simple; open a Terminal window and type
ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"
Once installed, just type
brew in a terminal to check that Homebrew is installed. There are few commands to remember:
Updating brew and formula (ie tools/components you have installed)
brew search ack
Installing / Uninstalling
brew install FORMULA brew uninstall FORMULA
brew install imagemagickwill install ImageMagick on your OS,
brew uninstall ackwill uninstall ack.
Listing installed formula
And finally, troubleshooting
brew doctor and if the output is
Your system is raring to brew, everything is ok. You can have more information on the Homebrew wiki.
2. Installing Python 3
To install Python 3 with Homebrew, simply type
brew install python3
Once Python 3 is installed, we’re going to check that is it ready to use or our system by launching the interactive console
And you should see
$ python3 Python 3.3.0 (default, Feb 1 2013, 22:09:55) [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
Type Ctrl+D to exit the interpreter. It’s interesting to note that
python launch the default 2.7 python interpreter and
python3 is launching the 3.3 python interpreter.
Now that we have Python 3 installed on our system, we are going to install a package. Python 3 comes with pip. You should pay attention to which pip you are using:
- pip is for Python 2.7
- pip3 is for Python 3
The two package managers don’t share their site-package directory. If you want a python package x for both pythons, you need to install it twice. Once via pip for Python 2.7 and once via pip3 for python 3.3.
If we want to install SQLAlchemy for instance, type
pip3 install sqlalchemy
Then launch the
python3 interactive console and test that SQLAlchemy is installed now
$ python3 Python 3.3.0 (default, Feb 1 2013, 22:09:55) [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import sqlalchemy >>>
No error, everything is OK! Note that, with Homebrew, Python 3 packages are installed under
3. Installing Eclipse
Now that we have Python 3 installed, we can use any good text editor to write some code. It can be also useful, but not mandatory, to use a complete IDE with Python autocompletion, debugging facilities and so on. To do so, we’re going to install Eclipse and PyDev.
To install Eclipse, go to http://www.eclipse.org/downloads/, choose Mac OSX (Cocoa) and download Eclipse Classic 4.2.1. Untar the downloaded file and copy the
eclipse directory under
4. Installing PyDev for Eclipse
PyDev is a Python IDE for Eclipse. To install it, launch Eclipse, go to ’Help > Install New Software’ (you can also follow these steps on PyDev online documentation)
Click on ’Add…’ then PyDev in ’Name’ and http://pydev.org/updates/ in ’Location’
Tap on ’OK’, then select PyDev in the combo box ’Work with:’
’Next’, select ’PyDev for Eclipse’, accept the Terms & Conditions, then ’Finish’.
Then accept the certificates and wait for Eclipse to restart.
After installing, we are going to configure PyDev to use our Python 3 interpreter.
- Go to ’Eclipse > Preferences > PyDev > Interpreter - Python’
- Select ’New’, in ’Interpreter Name’ set python3 and in ’Interpreter Executable’ set ’/usr/local/bin/python3’ (which is a symlink to
/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/bin/python3.3) then ’OK’
3. Just type OK on this window
Now, PyDev is configured and ready to use in Eclipse. To test it, go to ’File > New > Project…’ and select ’PyDev Project’, name your new project ’Test’, Project Type to ’Python’, Grammar Version to ’3.0’, interpreter to ’python3’, select ’Create ’src’ folder and add it to the PYTHONPATH’ and ’Finish’
Once the project is created, you can see on the left the PyDev Package Explorer with the Test project. Select the src folder, ’File > New > File’ and call it test.py. Copy/paste this code
if __name__ == ’__main__’: print(’Hello World’)
An run it from Eclipse (Run > Run). You can see the output directly in the Eclipse console. That’s all, you’re ready to code and debug! If you want to go further, check out the PyDev online documentation, it’s really good and complete.