# Python

## Python <a href="#python" id="python"></a>

macOS, like Linux, ships with [Python](https://python.org/) already installed. But you don't want to mess with the system Python (some system tools rely on it, etc.), so we'll install our own version(s). There are two ways to install Python, (1) Homebrew and (2) Pyenv. If you plan to use multiple versions of Python (e.g. 2, 3, and anaconda) then you should use pyenv.

### Installation <a href="#installation" id="installation"></a>

#### Homebrew method <a href="#homebrew-method" id="homebrew-method"></a>

Python 3 is the default version when installing with Homebrew, so if you want to install Python 2.7 you'll have to be explicit about it.

**Python 3**

```
$ brew install python
```

**Python 2.7**

```
$ brew install python@2
```

Installing Python also installs [pip](https://pypi.org/project/setuptools/) (and its dependency [Setuptools](https://pypi.python.org/pypi/setuptools)), which is the package manager for Python. Let's upgrade them both:

```
$ pip install --upgrade setuptools
$ pip install --upgrade pip
```

Executable scripts from Python packages you install will be put in `/usr/local/share/python`, make sure it's on your `PATH`.

#### Pyenv method <a href="#pyenv-method" id="pyenv-method"></a>

[`pyenv`](https://github.com/yyuu/pyenv) is a Python version manager that can manage and install different versions of Python. Works very much like `rbenv` for Ruby. First, we must install `pyenv` using Homebrew:

```
$ brew install pyenv
```

To upgrade `pyenv` in the future, use `upgrade` instead of `install`. After installing, add `pyenv init` to your shell to enable shims and autocompletion (use `.zshrc` if you're using `zsh`).

```
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
```

Restart your shell so the path changes take effect.

```
$ exec $SHELL
```

You can now begin using `pyenv`. To list the all available versions of Python, including Anaconda, Jython, PyPy and Stackless, use:

```
$ pyenv install --list
```

Then install the desired versions:

```
$ pyenv install 2.7.12
$ pyenv install 3.5.2
```

Use the `global` command to set global version(s) of Python to be used in all shells. For example, if you prefer 2.7.12 over 3.5.2:

```
$ pyenv global 2.7.12 3.5.2
$ pyenv rehash
```

The leading version takes priority. All installed Python versions can be located in `~/.pyenv/versions`. Alternatively, you can run:

```
$ pyenv versions
  system (set by /Users/your_account/.pyenv/version)
* 2.7.12
* 3.5.2
```

This shows an asterisk `*` next to the currently active version.<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://toska.gitbook.io/macos-setup/python.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
