|
| 1 | +.. _install3-osx: |
| 2 | + |
| 3 | +Installing Python 3 on Mac OS X |
| 4 | +================================ |
| 5 | + |
| 6 | +The latest version of Mac OS X, El Capitan, **comes with Python 2.7 out of the box**. |
| 7 | + |
| 8 | +You do not need to install or configure anything else to use Python 2. These |
| 9 | +instructions document the installation of Python 3. |
| 10 | + |
| 11 | +The version of Python that ships with OS X is great for learning but it's not |
| 12 | +good for development. The version shipped with OS X may be out of date from the |
| 13 | +`official current Python release <https://www.python.org/downloads/mac-osx/>`_, |
| 14 | +which is considered the stable production version. |
| 15 | + |
| 16 | +Doing it Right |
| 17 | +-------------- |
| 18 | + |
| 19 | +Let's install a real version of Python. |
| 20 | + |
| 21 | +Before installing Python, you'll need to install GCC. GCC can be obtained |
| 22 | +by downloading `XCode <http://developer.apple.com/xcode/>`_, the smaller |
| 23 | +`Command Line Tools <https://developer.apple.com/downloads/>`_ (must have an |
| 24 | +Apple account) or the even smaller `OSX-GCC-Installer <https://github.com/kennethreitz/osx-gcc-installer#readme>`_ |
| 25 | +package. |
| 26 | + |
| 27 | +.. note:: |
| 28 | + If you already have XCode installed, do not install OSX-GCC-Installer. |
| 29 | + In combination, the software can cause issues that are difficult to |
| 30 | + diagnose. |
| 31 | + |
| 32 | +.. note:: |
| 33 | + If you perform a fresh install of XCode, you will also need to add the |
| 34 | + commandline tools by running ``xcode-select --install`` on the terminal. |
| 35 | + |
| 36 | +While OS X comes with a large number of UNIX utilities, those familiar with |
| 37 | +Linux systems will notice one key component missing: a package manager. |
| 38 | +`Homebrew <http://brew.sh>`_ fills this void. |
| 39 | + |
| 40 | +To `install Homebrew <http://brew.sh/#install>`_, open :file:`Terminal` or |
| 41 | +your favorite OSX terminal emulator and run |
| 42 | + |
| 43 | +.. code-block:: console |
| 44 | +
|
| 45 | + $ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" |
| 46 | +
|
| 47 | +The script will explain what changes it will make and prompt you before the |
| 48 | +installation begins. |
| 49 | +Once you've installed Homebrew, insert the Homebrew directory at the top |
| 50 | +of your :envvar:`PATH` environment variable. You can do this by adding the following |
| 51 | +line at the bottom of your :file:`~/.profile` file |
| 52 | + |
| 53 | +.. code-block:: console |
| 54 | +
|
| 55 | + export PATH=/usr/local/bin:/usr/local/sbin:$PATH |
| 56 | +
|
| 57 | +Now, we can install Python 3: |
| 58 | + |
| 59 | +.. code-block:: console |
| 60 | +
|
| 61 | + $ brew install python3 |
| 62 | +
|
| 63 | +This will take a minute or two. |
| 64 | + |
| 65 | + |
| 66 | +Pip |
| 67 | +---------------- |
| 68 | + |
| 69 | +Homebrew installs ``pip3`` for you. |
| 70 | + |
| 71 | +``pip3`` is the alias for the Python 3 version of ``pip`` on systems with both |
| 72 | +the Homebrew'd Python 2 and 3 installed. |
| 73 | + |
| 74 | +Working with Python3 |
| 75 | +-------------------- |
| 76 | + |
| 77 | +At this point, you have the system Python 2.7 available, potentially the |
| 78 | +:ref:`Homebrew version of Python 2 <install-osx>` installed, and the Homebrew |
| 79 | +version of Python 3 as well. |
| 80 | + |
| 81 | +.. code-block:: console |
| 82 | +
|
| 83 | + $ python |
| 84 | +
|
| 85 | +will launch the Python 2 interpreter. |
| 86 | + |
| 87 | +.. code-block:: console |
| 88 | +
|
| 89 | + $ python3 |
| 90 | +
|
| 91 | +will launch the Python 3 interpreter |
| 92 | + |
| 93 | +``pip3`` and ``pip`` will both be available. If the Homebrew version of Python |
| 94 | +2 is not installed, they will be the same. If the Homebrew version of Python 2 |
| 95 | +is installed then ``pip`` will point to Python 2 and ``pip3`` will point to |
| 96 | +Python 3. |
| 97 | + |
| 98 | + |
| 99 | +Virtual Environments |
| 100 | +-------------------- |
| 101 | + |
| 102 | +A Virtual Environment (commonly referred to as a 'virtualenv') is a tool to keep |
| 103 | +the dependencies required by different projects in separate places, by creating |
| 104 | +virtual Python environments for them. It solves the "Project X depends on |
| 105 | +version 1.x but, Project Y needs 4.x" dilemma, and keeps your global |
| 106 | +site-packages directory clean and manageable. |
| 107 | + |
| 108 | +For example, you can work on a project which requires Django 1.10 while also |
| 109 | +maintaining a project which requires Django 1.8. |
| 110 | + |
| 111 | +To start using this and see more information: :ref:`Virtual Environments <virtualenvironments-ref>` docs. |
| 112 | + |
| 113 | +-------------------------------- |
| 114 | + |
| 115 | +This page is a remixed version of `another guide <http://www.stuartellis.eu/articles/python-development-windows/>`_, |
| 116 | +which is available under the same license. |
0 commit comments