Introduction
Python is both a popular beginner language, and a crucial part of Mint, for the OS to function properly. A common mistake for newcomers is to mess up their system-wide Python installation: removing it, replacing it with another one, downloading Python versions from PPAs, or even worse, downloading and building another version from source (which has no clear way of uninstalling). The results can be the loss of their desktop functionality, or worse.
The commands below are meant to be used in a Terminal window, using
bash
. Instructions are different for other shells. The python command
First, there is usually confusion about Python 2 and 3 versions/commands. Normally Python that comes with 64-bit Mint (20, 20.1, 20.2) is some version of Python 3.8. On Mint 21, it's some version of 3.10.
Code: Select all
python --version
Command 'python' not found, did you mean:
command 'python3' from deb python3
command 'python' from deb python-is-python3
python
refers to Python 2 whereas python3
refers to Python 3. That's default behavior on all Ubuntu based distros. This causes issues with some popular tools such as youtube-dl
or yt-dlp
which expect the python
command to use Python 3. This is such a common issue that Ubuntu created a package for it!Code: Select all
python3 --version
Python 3.8.10
On Mint 20 and above, Python 2 is absent by default, so it is not needed for the functioning of your operating system any more, so it's safe to redirect the
python
command to python3
. This won't mess with anything that's already using /usr/bin/python3
. So let's install that package that apt
tells us (obviously don't do this if you installed Python 2 on purpose for some reason!):Code: Select all
sudo apt install python-is-python3
Code: Select all
python --version
Python 3.8.10
Now both
python
and python3
commands will use Python 3. As long as you did not install Python 2 for some specific reasons, this is OK.Multiple Python version management with PyEnv
Managing multiple versions of programming languages is a common problem for developers, so they developed tools for it. There are many ways to do this, but one convenient way is PyEnv: https://github.com/pyenv/pyenv/ It won't interfere with your system Python or your operating system. It's basically a fancy way of using virtual environments.
To install PyEnv we need to install the dependencies first. These are needed to download and install PyEnv itself, and to download and build the alternate Python versions:
Code: Select all
sudo apt install make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm git libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
apt
message carefully before you hit Enter, just to be safe!The manual installation of PyEnv is quite complicated, so now we visit the automated installer at https://github.com/pyenv/pyenv-installer.
It tells us to download and run a script:
curl https://pyenv.run | bash
You should take a look at it to make sure it is safe: https://raw.githubusercontent.com/pyenv ... -installer It's open source, and it's examined by many people, so it should be safe, but take a look anyway. Definitely don't take my word for it.Let's do it:
Code: Select all
curl https://pyenv.run | bash
Now let's setup PyEnv, so that it's always available when we log in, and when we start a Terminal running
bash
. We need to add some lines to our ~/.bashrc
and our ~/.profile
files. To do this, run these 3 commands:
Code: Select all
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
Code: Select all
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
Code: Select all
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
~/.bashrc
that look like this (check with xed ~/.bashrc
):
Code: Select all
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
~/.profile
file. Run the commands:
Code: Select all
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.profile
Code: Select all
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile
Code: Select all
echo 'eval "$(pyenv init -)"' >> ~/.profile
Code: Select all
pyenv versions
* system (set by /home/spamegg/.pyenv/version)
We only have the system version of Python so far. Let's install another version of Python. We can list available versions:
Code: Select all
pyenv install --list
Code: Select all
pyenv install 3.12-dev
Code: Select all
pyenv versions
* system (set by /home/spamegg/.pyenv/version)
3.12-dev
~/.pyenv/versions/3.12-dev
. The star * is the one that is active in the Terminal. Let's switch it:Code: Select all
pyenv shell 3.12-dev
Code: Select all
python
Python 3.12.0a3+ (heads/main:7116030, Jan 7 2023, 17:54:20) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
To change back, we can use
pyenv shell system
. Check again:Code: Select all
python
Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Using Python versions easily inside IDEs
But you don't even have to manually change the Python version in the Terminal, if you are using an IDE! Visual Studio Code https://code.visualstudio.com/ and PyCharm Community Edition https://www.jetbrains.com/pycharm/downl ... tion=linux are two excellent free IDEs for Python coding.
You can easily choose your Python version in Visual Studio Code (first install the Python extension!). Click bottom left corner on the Python version, then choose the version you want from the drop down menu: Then the VS Code Integrated Terminal executes
pyenv shell 3.10.0
command for you automatically.And PyCharm (click bottom right corner, then "Add Interpreter"):
Going further
You can install as many other versions of Python with
pyenv install ...
where you replace ...
with any one of the versions that are listed in pyenv versions --list
. You can remove a Python version with pyenv uninstall ...
Obviously don't try pyenv uninstall system
! I haven't tried it, but it probably doesn't let you do that.You can update
pyenv
itself with the pyenv update
command. If you are sick of it, you can "uninstall" everything with rm -fr ~/.pyenv
. Once again, this is safe and it won't interfere with your system Python.The
pyenv shell ...
command's effect will expire when you close your Terminal, your Python version will revert back to what it was. If you want to make the changes stay, you have to use, for example, pyenv global 3.12-dev
. But this can be trouble if you forget to switch it back! Just do pyenv global system
and you are safe.The
pyenv shell ...
(and also the pyenv global ...
) commands can enable as many Python versions all at once as you want, if you are simultaneously testing your code on multiple Python versions (for example, running parallel tests with a testing tool such as tox
). I have 14 versions installed that I can test in parallel, super useful! Read up on PyEnv more on the Github page: https://github.com/pyenv/pyenv/