Python¶
Updated June 17, 2025
Create a virtual environment¶
brew
and admin rights required
To use the instructions below, you absolutely need admin rights AND brew
installed.
For the latter you can follow the instructions here. For the former, you need to request your manager/supervisor to petition for those rights for valid reasons. Once you have approval, contact the IT manager to switch your account.
If you see an error message when trying to run python3 or pip command that says this:
Error: externally-managed-environment
... then three options are available to you:
Best method
Run this command to create a virtual environment:
python3 -m venv venv
source venv/bin/activate
deactivate
Most convenient
See below, Managing multiple Local Python Versions
Discouraged
Alternatively, but not reccommended as it is less safe, you need to add this flag:
--break-system-packages
Managing multiple Local Python Versions¶
Working with multiple Python projects can be overwhelming, especially when switching from one Python runtime version to another. A good way to achieve a proper switch without breaking the ability to execute conflicting projects is to use Python environments.
Important
pyenv
is unaware of any Python installations it didn’t install itself. Therefore it is recommended to start with an environment where no version of Python is installed but the default OS Python installation.
Install pyenv
on macOS¶
brew
and admin rights required
To use the instructions below, you absolutely need admin rights AND brew
installed.
For the latter you can follow the instructions here. For the former, you need to request your manager/supervisor to petition for those rights for valid reasons. Once you have approval, contact the IT manager to switch your account.
Install dependencies¶
xcode-select --install
brew update && brew upgrade
brew install openssl readline sqlite3 xz zlib tcl-tk@8 libb2
Instal pyenv
via brew¶
brew update && brew install pyenv
Note
No Python version is installed with pyenv
, you need to install one manually.
See Install a specific version of Python or Install the most recent Python 3 version below.
Set up your shell environment for pyenv
¶
source ~/.zshrc
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init - zsh)"' >> ~/.zshrc
Install pyenv-virtualenv
on macOS¶
brew
and admin rights required
To use the instructions below, you absolutely need admin rights AND brew
installed.
For the latter you can follow the instructions here. For the former, you need to request your manager/supervisor to petition for those rights for valid reasons. Once you have approval, contact the IT manager to switch your account.
Next, install pyenv-virtualenv:
brew install pyenv-virtualenv
pyenv versions
Set up your shell environment for pyenv
¶
source ~/.zshrc
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
source ~/.zshrc
Check which virtual environments are installed¶
pyenv virtualenvs
Install a Python version¶
Install the target version you want if it’s not in the list, exemple:
pyenv install 3.10.5
Or install the target Python version in a virtual environment:
pyenv virtualenv 3.10.5 venv310
pyenv activate venv310
Then restart your shell for the PATH changes to take effect, or:
source ~/.zshrc
Uninstall a virtual environment¶
pyenv uninstall venv310
Useful commands¶
List available versions¶
pyenv install -l
Install the most recent Python 3 version¶
pyenv install 3
or...
pyenv install 3.10
Install a specific version of Python¶
pyenv install 3.10.4
Switch between Python versions¶
To select a pyenv
-installed Python as the version to use, run one of the following commands:
pyenv shell <version>
— select just for current shell sessionpyenv local <version>
— automatically select whenever you are in the current directory (or its subdirectories)pyenv global <version>
— select globally for your user account- … or use
pyenv-virtualenv
as described elsewhere in this page.
More information¶
https://github.com/pyenv/pyenv#how-it-works
Install Python 3 on Debian/Ubuntu¶
Install dependencies¶
sudo apt install software-properties-common build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
Install the latest version...¶
sudo apt install python3 -y
...or install a specific version.¶
sudo apt install python3.12 -y
Install the virtual environment plug-in¶
apt install python3-virtualenv -y
apt install python3.12-venv
Install pip¶
apt install python3-pip -y
python3 --version