Overview
Recent platforms (v6+) support multiple Python interpreters from the shell using pyenv. pyenv allows seamless switching between available Python versions, and manages version-specific package installations too.
Basics
All commands are done from the terminal.
Listing versions
To get a current list of available Python interpreters, use pyenv versions
:
Of importance is system
, which is the default system interpreter. Without pyenv installed, system
is the default interpreter used. 2.7.8
, 3.3.5
, and 3.4.1
are additional interpreters installed in addition to the system version under /.socket/python/versions
.
Selecting a version
A version may be defined per-directory and inherited recursively within all child directories. Versions are defined for a directory, and its child directories, by pyenv local
:
Two versions of Python are defined, one in /home/myadmin
and another in /var/www
. Versions are controlled by a file called .python-version
and pyenv will ascend each parent directory until it reaches root (/
) or .python-version
is found. If no control file is found, the current Python interpreter will be used.
You can then delegate a single Python version to handle a collection of projects by locating projects under an additional directory and defining a Python version in its parent directory:
www <-- NO .python-version |-- proj3 <-- .python-version (3.3.5) | |-- mydjango (3.3.5) | `-- flask (3.3.5) `-- proj2 <-- .python-version (2.7.5) |-- newdjango (2.7.5) `-- mezzanine (2.7.5)
Control files are located under /var/www/proj3
and /var/www/proj2
allowing projects located under these directories to use Python 3 or Python 2 respectively.
Package locations
Packages are installed using pip as normal. Packages are located under /usr/local/lib/python/<MAJOR>.<MINOR>.<PATCH>
using semantic versioning to avoid conflict. Any binaries installed under /usr/local/bin, however, will conflict with each other if those binaries, bundled in different versions of the package, differ. pyenv-virtualenv + virtualenv is recommended to isolate packages of different versions that rely on the same Python interpreter.
Caveats
Shebangs
Shell or FastCGI scripts typically begin with #!/usr/bin/python
. This is the system version that will bypass pyenv initialization. Change this line to #!/usr/bin/env python
to use python found in the shell path.
See also
- pyenv github
- virtualenv (manage same Python version, different package versions)