[Tutorial] Safely install and use other Python versions for coding (Mint 20 and 21, 64-bit)

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
User avatar
spamegg
Level 13
Level 13
Posts: 4862
Joined: Mon Oct 28, 2019 2:34 am
Contact:

[Tutorial] Safely install and use other Python versions for coding (Mint 20 and 21, 64-bit)

Post by spamegg »

Last updated: Apr 18, 2023
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
Oops! This is because 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
This will be something like 3.10.6 on Mint 21.

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
This created a symlink from /usr/bin/python to /usr/bin/python3. Let's try again:

Code: Select all

python --version
Python 3.8.10
(Again, this will be something like 3.10.6 on Mint 21.)

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
Normally these libraries should not conflict or remove anything, but read the 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
Setting up PyEnv for your profile and shell
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
This added 3 new lines at the end of ~/.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 -)"
Now we have to add the same lines to our ~/.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
Logout, log back in. Open a Terminal. Let's check things:

Code: Select all

pyenv versions
* system (set by /home/spamegg/.pyenv/version)
Installing other Python versions with PyEnv
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
Wow, there are A LOT of versions! Let's go with 3.12-dev:

Code: Select all

pyenv install 3.12-dev
Now let's check again:

Code: Select all

pyenv versions
* system (set by /home/spamegg/.pyenv/version)
  3.12-dev
Nice. It gets installed to the directory ~/.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
Now let's try to use Python:

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.
>>>
Nice! Exit Python shell with Ctrl+D.

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.
>>>
(Again, this will be something like 3.8.10 on Mint 20.)

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:
vsc2.png
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"):
pyc1.png
pyc1.png (8.09 KiB) Viewed 20877 times
pyc2.png

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/
Last edited by spamegg on Tue Apr 18, 2023 4:27 pm, edited 5 times in total.
User avatar
Yam1598
Level 1
Level 1
Posts: 4
Joined: Wed Sep 21, 2022 5:33 pm

Re: [Tutorial] Safely install and use other Python versions for coding (Mint 20 64-bit)

Post by Yam1598 »

worked like a charm

in VS Code, after setting it up, restart terminal to get the magic switching happening.
User avatar
xiamu
Level 1
Level 1
Posts: 6
Joined: Sat Mar 18, 2023 8:16 pm

Re: [Tutorial] Safely install and use other Python versions for coding (Mint 20 and 21, 64-bit)

Post by xiamu »

I need a translator to read it!

Thanks for the help, I'll try it later!
User avatar
xiamu
Level 1
Level 1
Posts: 6
Joined: Sat Mar 18, 2023 8:16 pm

Re: [Tutorial] Safely install and use other Python versions for coding (Mint 20 and 21, 64-bit)

Post by xiamu »

I followed your steps to install, and the pyenv version output reports an error, I don't know why?

I used the following settings, logged out, and reopened! Everything works fine!

linux mint 21.1

Code: Select all


vim ~/.bashrc

# 文件最后添加
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
export PATH="$PYENV_ROOT/shims:$PATH"
eval "$(pyenv init -)

User avatar
spamegg
Level 13
Level 13
Posts: 4862
Joined: Mon Oct 28, 2019 2:34 am
Contact:

Re: [Tutorial] Safely install and use other Python versions for coding (Mint 20 and 21, 64-bit)

Post by spamegg »

xiamu wrote: Sun Mar 19, 2023 10:25 pm I followed your steps to install, and the pyenv version output reports an error, I don't know why?
PyEnv seems to have slightly changed their instructions recently. They require adding the 3 lines to both ~/.bashrc and to ~/.profile now. I just updated the tutorial with the new instructions (also applied them to my own PC).
I used the following settings, logged out, and reopened! Everything works fine!
Great! I'm glad it worked out. Not sure about the shims directory you added there. It works for me without adding it to PATH. It even works on my other zsh Terminal.
User avatar
SMG
Level 25
Level 25
Posts: 31333
Joined: Sun Jul 26, 2020 6:15 pm
Location: USA

Re: [Tutorial] Safely install and use other Python versions for coding (Mint 20 and 21, 64-bit)

Post by SMG »

Moderator note: zanshinzan's issue can now be found here Problems on LM19.2 with tutorial to safely install and use other Python versions for coding (Mint 20 and 21, 64-bit) because support questions should not be added to tutorials and this tutorial specifically says it is for Mint 20 and 21 (not Mint 19).
Image
A woman typing on a laptop with LM20.3 Cinnamon.
Polemos
Level 1
Level 1
Posts: 1
Joined: Thu Jul 06, 2023 5:16 am

Re: [Tutorial] Safely install and use other Python versions for coding (Mint 20 and 21, 64-bit)

Post by Polemos »

Just a suggestion for the guide, in case that someone wishes to align with the XDG specs and use a different directory than .pyenv (to avoid cluttering $HOME).

This can be accomplished by having the following set in `.bashrc`, before initiating the installation process.
  • For example to use .config/pyenv instead:

    Code: Select all

    export PYENV_ROOT=$HOME/.config/pyenv
    
  • Or, if you have the XDG variables set (.profiles for instance):

    Code: Select all

    export PYENV_ROOT=$XDG_DATA_HOME/pyenv
    
Post Reply

Return to “Tutorials”