Doing it Right¶. Let’s install a real version of Python. Before installing Python, you’ll need to install a C compiler. The fastest way is to install the Xcode Command Line Tools by running xcode-select-install.
Python 3 (released in 2008) is the newest version of Python, and most features have not changed. Most packages have been updated to Python 3 by now (2016). So, if your lab does not have a preference, I recommend using Python 3. There are a couple key differences for novice programmers: In Python 2, you can print with print 42 or print(42). For OSX users, python2.7 comes pre-installed in /usr/bin/ but any python versions that were downloaded and installed by a user are likely in /usr/local/bin/ (see OutOnAWeekend's comment). – David Skarbrevik Oct 12 '18 at 22:10. As of 2/1/2021 Python 3.4 and 3.5 is deprecated. Python 2.7 was deprecated by the Python Software Foundation on January 1, 2020. Going forward, customers using the AWS CLI version 1 should transition to using Python 3, with a minimum of Python 3.6.
Note
Check out our guide for installing Python 3 on OS X.
Mac OS X comes with Python 2.7 out of the box.
You do not need to install or configure anything else to use Python. Having saidthat, I would strongly recommend that you install the tools and librariesdescribed in the next section before you start building Python applications forreal-world use. In particular, you should always install Setuptools, as it makesit much easier for you to install and manage other third-party Python libraries.
The version of Python that ships with OS X is great for learning, but it’s notgood for development. The version shipped with OS X may be out of date from theofficial current Python release,which is considered the stable production version.
Doing it Right¶
Let’s install a real version of Python.
Before installing Python, you’ll need to install a C compiler. The fastest wayis to install the Xcode Command Line Tools by runningxcode-select--install
. You can also download the full version ofXcode from the Mac App Store, or theminimal but unofficialOSX-GCC-Installerpackage.
Note
If you already have Xcode installed, do not install OSX-GCC-Installer.In combination, the software can cause issues that are difficult todiagnose.
Note
If you perform a fresh install of Xcode, you will also need to add thecommandline tools by running xcode-select--install
on the terminal.
While OS X comes with a large number of Unix utilities, those familiar withLinux systems will notice one key component missing: a decent package manager.Homebrew fills this void.
To install Homebrew, open Terminal
oryour favorite OS X terminal emulator and run
The script will explain what changes it will make and prompt you before theinstallation begins.Once you’ve installed Homebrew, insert the Homebrew directory at the topof your PATH
environment variable. You can do this by adding the followingline at the bottom of your ~/.profile
file
Now, we can install Python 2.7:
Because python@2
is a “keg”, we need to update our PATH
again, to point at our new installation:
Homebrew names the executable python2
so that you can still run the system Python via the executable python
.
Setuptools & Pip¶
Homebrew installs Setuptools and pip
for you.
Setuptools enables you to download and install any compliant Pythonsoftware over a network (usually the Internet) with a single command(easy_install
). It also enables you to add this network installationcapability to your own Python software with very little work.
pip
is a tool for easily installing and managing Python packages,that is recommended over easy_install
. It is superior to easy_install
in several ways,and is actively maintained.
Virtual Environments¶
A Virtual Environment (commonly referred to as a ‘virtualenv’) is a tool to keep the dependencies required by different projectsin separate places, by creating virtual Python environments for them. It solves the“Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keepsyour global site-packages directory clean and manageable.
For example, you can work on a project which requires Django 1.10 while alsomaintaining a project which requires Django 1.8.
To start using this and see more information: Virtual Environments docs.
This page is a remixed version of another guide,which is available under the same license.
- Set up your computer
- Practice
Start here!
Osx Update Python 2.7 To 3
Try this 10-minute tutorial. When itloads, type tutorial
and press Enter to start.
Set up your computer
This is our recommended way to install Python on your system.
Install Anaconda
- Please download the Anaconda installer. We recommend Python 3.
- Choose
Install for me only
- By default, Anaconda will prepend itself to your PATH – leave this as is
- When Anaconda has finished installing, open a terminal (Linux, OSX), or the Anaconda Prompt (Windows)
- Type
conda update conda
, hit enter, and then type 'y' (and hit enter) - Type
conda update anaconda
, hit enter, and then type 'y' (and hit enter)
Run the Jupyter Notebook
With Jupyter, you intersperse code, output, explanatory text, and figures in one big file called a 'notebook.' Notebooks are a convenient format to explore a language and to share examples of code.
- To run a notebook, open the Terminal (Linux, OSX) or Anaconda Prompt (Windows) and type
jupyter notebook
. - The notebook will open a new tab in your default browser. Do not close the terminal, as this will also shutdown the notebook.
- When it has loaded, click on 'New' (at the top right) and then 'Python3' to create a new notebook.
Python 2 vs Python 3
Python 3 (released in 2008) is the newest version of Python, and most featureshave not changed. Most packages have been updated to Python 3 by now (2016).So, if your lab does not have a preference, I recommend using Python 3.
There are a couple key differences for novice programmers:
- In Python 2, you can print with
print 42
orprint(42)
. In Python3, you need to use parentheses, as inprint(42)
. In Python 2, division of two integers like
5/2
will evaluate to2
. (Python will drop the remainder if both numbers are integers.)Python 3 does exact division ('2.5', in this example). If you use Python 2 and do not want this behavior, add this line at the top of each program:from __future__ import division
.
Text editors and IDEs
For creating large projects in Python, we recommend using a text editor in combination with the Jupyter notebook. Popular choices include:
For even larger projects, a well-engineered IDE (Interactive Development Environment) may be better than a text editor. Typically, IDEs include drag-and-drop support for debugging and refactoring. Popular choices include:
Practice
Past topics of the Python Working Group are linked here. Suggestions? Send us an email at dlab-frontdesk@berkeley.edu.
Osx Upgrade Python
Software Carpentry
These exercises are really useful to get acquainted with Python. Here's a link to the Jupyter notebook version and the webpage version.
Python for Social Sciences
This is a free online book by Jean Mark Gawron. It's free and online at this link.
Resources
Mac Update Python
Check the Learning Resources page for more learning materials. Others in the community may also have relevant materials.