banner



How To Install Python In Linux Mint

Last updated: Dec 02, 2022
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-broad Python installation: removing it, replacing it with another ane, downloading Python versions from PPAs, or fifty-fifty worse, downloading and building another version from source (which has no clear way of uninstalling). The results tin be the loss of their desktop functionality, or worse.

The commands beneath are meant to be used in a Terminal window, using bash. Instructions are dissimilar for other shells. (You don't need to type the arrow ➜ grapheme, that's only for testify.)

The python command
First, there is usually defoliation almost Python 2 and 3 versions/commands. Ordinarily Python that comes with 64-bit Mint (20, xx.1, xx.2) is some version of Python 3.8.

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 three. That'southward default behavior on all Ubuntu based distros. This causes problems with some pop tools such as youtube-dl or yt-dlp which await the python command to use Python 3. This is such a mutual issue that Ubuntu created a bundle for information technology!

Code: Select all

                          ➜ python3 --version Python iii.8.10                      

On Mint xx and above, Python 2 is absent by default, so it is not needed for the functioning of your operating organization any more, then it'due south 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 exercise this if yous installed Python ii on purpose for some reason!):

Lawmaking: Select all

            sudo apt install python-is-python3                      

This created a symlink from /usr/bin/python to /usr/bin/python3. Let's try again:

At present both python and python3 commands will utilise Python iii. Every bit long equally you lot 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. At that place are many means to do this, simply 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 nosotros need to install the dependencies kickoff. These are needed to download and install PyEnv itself, and to download and build the alternate Python versions:

Lawmaking: Select all

            sudo apt install make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget gyre llvm git libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev                      

Normally these libraries should non conflict or remove anything, but read the apt message carefully before you striking Enter, just to be safe!

The transmission installation of PyEnv is quite complicated, and so at present nosotros visit the automated installer at https://github.com/pyenv/pyenv-installer.

It tells u.s. to download and run a script: curl https://pyenv.run | bash You lot should take a look at it to make sure it is safe: https://raw.githubusercontent.com/pyenv ... -installer Information technology'due south open source, and information technology's examined by many people, so it should be safe, but take a expect anyway. Definitely don't have my word for it.

Allow'south practice it:

Setting upwards PyEnv for your contour and shell
At present let's setup PyEnv, so that information technology's e'er available when we log in. Let's open the ~/.profile file:

Add these three lines to the end:

Code: Select all

            consign PYENV_ROOT="$Habitation/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init --path)"                      

Salve, shut. Open ~/.bashrc:

Add together this one line to the end:

Salve, close. Logout, log back in. Open a Final. Let's bank check things:

Lawmaking: Select all

            ➜ pyenv versions * system (set by /home/spamegg/.pyenv/version)                      

Installing other Python versions with PyEnv
We only have the arrangement version of Python and so far. Let's install another version of Python. Nosotros can list available versions:

Wow, there are A LOT of versions! Let'south get with 3.10.0:

Now permit'southward bank check again:

Code: Select all

                          ➜ pyenv versions * system (set by /home/spamegg/.pyenv/version)   three.10.0                      

Overnice. It gets installed to the directory ~/.pyenv/versions/three.10.0. The star * is the one that is active in the Terminal. Let'south switch it:

Now let's try to utilise Python:

Lawmaking: Select all

                          ➜ python Python 3.10.0 (default, December 2 2022, 19:03:52)                      

Dainty! Exit Python shell with Ctrl+D.

To change back, we can employ pyenv beat system. Cheque once more:

Lawmaking: Select all

                          ➜ python Python 3.8.10 (default, Sep 28 2022, sixteen:10:42)                      

Using Python versions easily inside IDEs

But you don't fifty-fifty accept to manually change the Python version in the Final, if y'all are using an IDE! Visual Studio Code https://code.visualstudio.com/ and PyCharm Community Edition https://www.jetbrains.com/pycharm/downl ... tion=linux are ii excellent complimentary IDEs for Python coding.

You tin easily choose your Python version in Visual Studio Code (starting time install the Python extension!). Click bottom left corner on the Python version, then choose the version you desire from the drib down menu:

vsc2.png

Then the VS Lawmaking Integrated Final executes pyenv shell three.10.0 command for you automatically.

And PyCharm (click lesser right corner, then "Add Interpreter"):

pyc1.png
pyc1.png (8.09 KiB) Viewed 1449 times
pyc2.png

Going further
You can install as many other versions of Python with pyenv install ... where you replace ... with whatsoever ane of the versions that are listed in pyenv versions --list. You can remove a Python version with pyenv uninstall ... Patently don't try pyenv uninstall organization! I haven't tried it, but information technology probably doesn't let you exercise 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 condom and it won't interfere with your system Python.

The pyenv crush three.10.0 command'southward effect will elapse when you lot close your Terminal, your Python version will revert back to what information technology was. If yous want to make the changes stay, yous take to utilize pyenv global three.ten.0. Simply this tin exist trouble if you forget to switch it dorsum! Simply exercise pyenv global organisation and you are safe.

The pyenv shell ...(and also the pyenv global ...) commands tin enable every bit many Python versions all at once equally you desire, 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 folio: https://github.com/pyenv/pyenv/

Source: https://forums.linuxmint.com/viewtopic.php?t=362499

Posted by: williamsontooll1944.blogspot.com

0 Response to "How To Install Python In Linux Mint"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel