I am planning to learn more about (web application) development with python. Today I learned how to
- Manage multiple python installations on the same system and
- Create virtual envirnments with pyenv.
There is a lot of information about virtual environments in python out there, e.g. in the official python documentation. In addition to managing dependencies for a project, I also foresee the need to manage different versions of python itself.
To set up my development environment, I decided to follow the advice of Real python and The hitchhiker’s guide to python and manage multiple python versions and virtual environments with pyenv
Installing pyenv on MX Linux
I used homebrew to install pyenv
on my linux machine running MX 23.1 Libretto.
brew update
brew install pyenv
Afterwards, I executed the following code to add three lines to my ~/.bashrc
file, as recommended on the pyenv README page.
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
Installing python with pyenv
Next, I used pyenv
to install the latest release version of python, version 3.12.1
(at the time of writing).
Prerequisites
To install successfully, python 3.12.1 required the following system tools to be available (installed via sudo apt install
):
libsqlite3-dev
tk-dev
Troubleshooting
I ran into the issue reported here, preventing me from installing python with pyenv
(see below). It turns out that brew masks the system’s pkg-config
, intefering with the compilation of python.
I unlinked pkg-config
with the following command, and the installation succeeded:
brew unlink pkg-config
Installing python version 3.12.1
First, I listed the (many) python versions pynev
is aware of:
pyenv install --list
and then installed the version of my choice:
pyenv install -v 3.12.1
Setting the default python version
Finally, I set my new python installation as the default version:
pyenv global 3.12.1
Creating and using virtual environments
To make using virtual environments with pyenv
easier, I installed the pyenv-virtualenv
plugin:
brew install pyenv-virtualenv
Creating a first virtual environment
The pyenv virtualenv
command creates a new virtual environment for the specified python version (e.g. 3.12.1).
pyenv virtualenv 3.12.1 rango
All virtual environments created in this way are stored in the same location, by default the ~/.pyenv/versions
folder:
ls ~/.pyenv/versions/
Activating a virtual environment
The following command activates my new rango
virtual environment:
pyenv activate rango
python --version # 3.12.1
and the equivalent command deactivates it:
pyenv deactivate rango
Using virtual environment in VS Code
To use my virtual environment in the VS Code IDE:
- Open VSCode preferences (Ctrl + ,)
- Search for
venv
. - Add ~/.pyenv to the “Venv Path” text box.
Once I created a first python file in VS Code, I could choose the python interpretor from the rango
virtual environment by clicking on the python version in the bottom right of the IDE window.
This work is licensed under a Creative Commons Attribution 4.0 International License.