Python Installation

1. System-wide Python installation

Python is usually installed in most modern OSs. However, if this is not true in your case, this section can help you.

MS Windows

Download Python from https://www.python.org/downloads/ ... and remember to select the option "Add Python to PATH".

Mac OS X

Install Homebrew and run

brew install python3

GNU Debian

Run

sudo apt-get update && sudo apt-get install python3

Arch Linux

Run

sudo pacman -Sy && sudo pacman -S python

2. pip: installing Python packages

pip is a command-line tool for accesing to PyPI - the standard Python's packages repository.

Get the list of installed packages

pip list
pip freeze

Searching for a package in PyPI

pip search games | grep free

Installing a package

pip install freegames        # System- or environment-wide installation (see below)
pip install --user freegames # Local installation (see $PYTHONUSERBASE, if set)
pip install Pillow==5.1.0    # Installing a version of a package
pip install numpy>=0.2       # Installing a version higher or equal to
pip install pandas<0.4       # Installing a version smaller than
pip install cv2>=0.2,<0.3    # Using a range of versions

Uninstalling a package

pip uninstall freegames

Getting information about an installed package

pip show freegames

Updating a package

pip install --upgrade freegames

Upgrading all packages

pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip install -U

Activating command completion

pip completion --bash >> ~/.profile
pip completion --zsh >> ~/.zprofile

Only temporaly:

bash$ eval "`pip completion --bash`"
zsh$ eval "`pip completion --zsh`"

3. Python (virtual) environments

A virtual environment is "a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages". This is useful when, for example, two or more Python projects that run in the same host require different configuration of Python and packages versions.

A. Using venv

Creating an environment

python -m venv my_python_project

Activating the environment

source my_python_project/bin/activate

Your prompt should have the prefix (my_python_project). The my_python_project directory should not be moved, and each different project should have its own environment.

Deactivation

deactivate

B. Simple Python Version Management: pyenv

Useful to install and use different versions of Python which are independent of the system's installed Python.

Install in all Unixes (included OSX)

(Replace .bashrc by .bash_profile in OSX)

git clone https://github.com/pyenv/pyenv.git ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bashrc

Source configuration files

exec "$SHELL"

List available python versions

pyenv install --list

Compile Python

Prerequisites

Debians

See Common build problems if you are unable to install an environment.

Moreover, in order to have tkinter graphical interface avaiable, the tk-dev Linux package should have been installed.

sudo apt-get install tk-dev
OSX

TODO

Arch Linux
pacman -S gcc make

Download and compile

pyenv install 3.6.4

Uninstall

Simply, remove the lines added to the configuration file and run:

rm -fr ~/.pyenv

Lists all Python versions known to pyenv

pyenv versions

Activate/Select a installed version of Python

pyenv local/global/shell 3.6.1

Deactivate

pyenv local/global/shell system

Displays the currently active Python version

pyenv version