Python is a high-level, interpreted programming language known for its simplicity and readability. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming. Python is widely used in web development, data science, automation, artificial intelligence, and scientific computing due to its extensive libraries and active community.
This article is a basic guide on the common setups one has to deal with in Python.
Linux/UNIX
Your system should already has a global Python version available. Run
python3 --version
to verify the version. The global python version should update itself with the package manager.
You can install other versions by compiling from source. Visit the Python Source Download Page for more information. Make sure to install in your system the necessary build dependencies.
On Arch Linux:
pacman -S --needed base-devel openssl zlib xz tk
Windows
Go to the Python Downloads Page and get the executable for the version you are interested it. Open the executable and follow the installation wizard. Donāt forget to check the Add Python to PATH.
A virtual environment in Python is an isolated workspace that allows you to manage dependencies separately for different projects. It prevents conflicts between packages by creating a self-contained directory with its own Python interpreter and libraries. Python >3.11 prevents users from installing packages globally via pip and are encouraged to to use virtual environments or user installs
pip install --user <package>
For better or for worse, there are several ways to create a virtual environment, the following sections will give an overview on the ones I (the main maintainer) have used in the past.
venv modulePython, has a built-in venv module for creating virtual environments. It is slow and limited, but if you donāt have access to fancy tools in a server that is not yours, then it can come in handy to know it exists.
python -m venv <path/to/vritual/environment>
For example, in one of the High Performance Computing (HPC) servers I have access, there are multiple python versions callable by e.g. python3.12 or python3.8, but we donāt have access to virtualenv. Thus I need to manage virtual environments either with conda or venv. To do this, call the the python binary you are interested in, followed by the module flag -m venv
python3.12 -m venv .venv
To activate any virtual environment, you need to
sourcetheactivatebinary, e.g.source .venv/bin/activateIf using the
fishshell, then use theactivate.fishbinary.
To exit any virtual environment, at any time run the command
deactivate.
virtualenv toolLet us create a virtual environment for general usage under the directory ~/.virtualenvs/. First install the system package python-virtualenv
sudo pacman -S python-virtualenv
mkdir ~/.virtualenvs/
As an example, letās make a virtualenv called pyglobal and activate it via the source command.
virtualenv .venv
source ~/.venv/bin/activate
Conda, either from Anaconda or Miniconda is arguably one of the most popular tools for creating virtual environments of managing versions (particularly among developers on MacOS).
To create a virtual environment, run
conda create -n <environment name> python=<version>
Note that we do not need to provide a path for the environment, as conda typically stores then in some sort of āglobalā location, typically under ~/.conda.
Different projects may require different Python versions due to compatibility with specific libraries, dependencies, or system requirements. For example, an older project might rely on python 3.7 because some of its dependencies have not been updated for newer versions, while a new project could require python 3.11 to take advantage of performance improvements and new language features.
Additionally, some systems or frameworks may only support certain Python versions, making it necessary to switch between them. Tools like pyenv or conda allow users to manage multiple Python versions efficiently, ensuring that each project runs in a controlled and compatible environment.
For rolling-releases, it is better to always use virtual environments that do not point to the global python version. When the global python is updated, your virtualenv might break.
pyenv toolPyenv is a tool, that among other things, it serves as an automation tool for compiling python versions from source and have them under the userās home directory ~/.pyenv/. For specific installation follow their installation guide, for Arch-based distributions you can find it on the extra repository, thus
sudo pacman -S pyenv
After you set up your shell, and install the python build dependencies, you can install a python version with
pyenv install <x.y.z>
where <x.y.z> is some python version e.g. 3.12.0. This command will compile and install the binaries under the local directory $HOME/.pyenv/versions/<x.y.z>/.
Once a python version is installed, we can let e.g. virtualenv point to those binaries.
virtualenv --python=$HOME/.pyenv/versions/<x.y.z>/bin/python<x.y> /path/to/venv
or if virtualenv is not available, you can directly call the venv module from the installed python binaries.
When conda is available, then it becomes really simple to choose a python version and create a virtual environment
conda create -n <environment name> python=<version>