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
Let 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 ~/.virtualenvs/pyglobal
source ~/.virtualenvs/global/bin/activate
For easier access, add an alias to your ~/.bashrc
.
alias pyglobal="source ~/.virtualenvs/global/bin/activate"
To exit any virtual environment, at any time run the command deactivate
.
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.
Tip: For rolling-releases, it is better to use virtual environments that do not point to the global python version. When the global python is updated, your virtualenv might break.
Pyenv 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 installation follow their documentation.
Once a python version is installed, we can let virtualenv
point to those binaries. As an example, here we installed Python 3.12.0 and created a virtual environment called py3.12
under the ~/.virtualenvs/
directory.
virtualenv --python=/home/USER/.pyenv/versions/3.12.0/bin/python3.12 /home/USER/.virtualenv/py3.12
Warning: It is really important to give the full path to the python binaries. Otherwise it can throw you a RuntimeError.