[SOLVED] Using newest Python versions (Or: "When One plays with a Python, One usually gets bit...lol.")

Quick to answer questions about finding your way around Linux Mint as a new user.
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions use the other forums in the support section.
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
Luke Johnson
Level 1
Level 1
Posts: 38
Joined: Sat Jun 12, 2021 2:44 pm

[SOLVED] Using newest Python versions (Or: "When One plays with a Python, One usually gets bit...lol.")

Post by Luke Johnson »

One of the reasons I'm switching from Windows to Linux is being able to use the newest versions of Python.

Is it possible to use new versions of Python in Mint without risking OS failures and other bad stuff? (See viewtopic.php?f=90&t=346847)

If it isn't possible in Mint, what distro is it possible in? Because this would be a dealbreaker.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
JoeFootball
Level 13
Level 13
Posts: 4673
Joined: Tue Nov 24, 2009 1:52 pm
Location: /home/usa/mn/minneapolis/joe

Re: Using newest Python versions (Or: "When One plays with a Python, One usually gets bit...lol.")

Post by JoeFootball »

Luke Johnson wrote: Is it possible to use new versions of Python in Mint without risking OS failures and other bad stuff?
I wouldn't try it on my "main" deployment of Mint, but I'd give it a go on a separate "test" partition. This way, if doing so does corrupt the OS, you've not lost anything.

Just fyi, the current version of Python on LM 20.x is 3.8.10. So, unless there's specifically something you must have that's only in 3.9.x, I'd just go with what's in the repos. Your call. :)
jonau01
Level 3
Level 3
Posts: 177
Joined: Tue Apr 07, 2020 4:01 pm

Re: Using newest Python versions (Or: "When One plays with a Python, One usually gets bit...lol.")

Post by jonau01 »

Yes you can, as long as you install it in your personal directory .

Don't change the default python version that comes with Linux mint.

Last thought : how to you think that linux mint develops and tests their version ? By using another OS ?
Last edited by jonau01 on Tue Jul 27, 2021 4:16 pm, edited 1 time in total.
NGRhodes
Level 1
Level 1
Posts: 18
Joined: Wed Jul 15, 2020 7:52 am

Re: Using newest Python versions (Or: "When One plays with a Python, One usually gets bit...lol.")

Post by NGRhodes »

Anaconda is great for this.
It is user installed (not sudo/root), runs its own versions of python, so won't interfere with the OS at all, you can create environments to run multiple versions of python and you can use anaconda package manager or still use pip. Anaconda supports other languages within same environments beyond python and used a lot in research and data science.
Another option is pyenv which allows environments similar to anaconda, this needs to be installed by the system, but the environments are purely user and separate from any system installed python (like with Anaconda)
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: Using newest Python versions (Or: "When One plays with a Python, One usually gets bit...lol.")

Post by xenopeek »

It's fine to install multiple Python versions on Linux Mint. What is not fine is replacing your system's default Python version (that is, leave the file /usr/bin/python3 untouched) or uninstalling it.

If you're a Python programmer I'm not telling you anything new that newer versions of Python can have different behavior (think dict insertion order) or have other changes that requiring porting earlier Python code. See the release notes of any version, which details such changes and points of attention.

All the Python software in the software repositories is tested with the system's default Python version. Hence don't change the default Python version.

Python 3.8.5 is I think current on Linux Mint 20. But you can additionally install Python 3.9.5 (apt install python 3.9) and to use that in any of your scripts start with hashbang line #!/usr/bin/python3.9 or #!/usr/bin/env python3.9 if using virtualenv. There are also PPAs (additional software repository) you can add to your system to get even newer (beta) versions of Python if you must have that. For example https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa lets you install 3.9.6 or 3.10 beta. Don't remove Python 3.8 or change the file /usr/bin/python3 and you should be fine.
Image
User avatar
Luke Johnson
Level 1
Level 1
Posts: 38
Joined: Sat Jun 12, 2021 2:44 pm

Re: Using newest Python versions (Or: "When One plays with a Python, One usually gets bit...lol.")

Post by Luke Johnson »

I need new versions. 3.9 has str.removeprefix(prefix) and str.removesuffix(suffix), among other things. I do a lot of string stuff.

3.10 will have structural pattern matching and I was really looking forward to experimenting with it.

Not to mention the versions which will come later. They could bring really useful stuff.

I'd rather not use Anaconda. I'm not sure I like what I'm reading about it. Besides, I'm used to CPython.

If installing the newest Python version in a personal directory is 100% safe and doesn't restrict my use of Python, I'd try. That option was ridiculed in the thread I posted, though.

I'd easily give up on Mint if I could find a stable distro where I don't have to worry and compromise in this area.
NGRhodes
Level 1
Level 1
Posts: 18
Joined: Wed Jul 15, 2020 7:52 am

Re: Using newest Python versions (Or: "When One plays with a Python, One usually gets bit...lol.")

Post by NGRhodes »

Luke Johnson wrote: Tue Jul 27, 2021 4:39 pm I need new versions. 3.9 has str.removeprefix(prefix) and str.removesuffix(suffix), among other things. I do a lot of string stuff.

3.10 will have structural pattern matching and I was really looking forward to experimenting with it.

Not to mention the versions which will come later. They could bring really useful stuff.

I'd rather not use Anaconda. I'm not sure I like what I'm reading about it. Besides, I'm used to CPython.

If installing the newest Python version in a personal directory is 100% safe and doesn't restrict my use of Python, I'd try. That option was ridiculed in the thread I posted, though.

I'd easily give up on Mint if I could find a stable distro where I don't have to worry and compromise in this area.
Cpython is just the reference implementation of Python, used by most of the computing world (including Anaconda, and all Linux I know of).
If you only need the one version for one project at a time, installing into your home directory is not terrible, just becomes harder to manage as you create more projects and start needing different versions of python.
ajgringo619

Re: Using newest Python versions (Or: "When One plays with a Python, One usually gets bit...lol.")

Post by ajgringo619 »

Luke Johnson wrote: Tue Jul 27, 2021 3:50 pm One of the reasons I'm switching from Windows to Linux is being able to use the newest versions of Python.
Not advocating for Windows over Mint, but installing the latest Python in Windows is simple. Take a look at Chocolatey - https://chocolatey.org/. Makes installing software on Windows (almost) as easy as installing software on Linux.

That being said, if you really want to segregate your different version of Python, you can compile each version yourself, install them in a directory structure something like /opt/python3.x.x, then build your virtual environments from there. This is obviously more work; I've used the methods already posted and they work great.
User avatar
Luke Johnson
Level 1
Level 1
Posts: 38
Joined: Sat Jun 12, 2021 2:44 pm

Re: Using newest Python versions (Or: "When One plays with a Python, One usually gets bit...lol.")

Post by Luke Johnson »

I'm used to working in the Atom text editor. Will I be able to install Python 3.9 safely and use it in Atom?
User avatar
spamegg
Level 14
Level 14
Posts: 5117
Joined: Mon Oct 28, 2019 2:34 am
Contact:

Re: Using newest Python versions (Or: "When One plays with a Python, One usually gets bit...lol.")

Post by spamegg »

Hello Luke,

You don't have to install Python 3.9 or 3.10 system-wide, in order to use them. You can use an IDE such as PyCharm that can install those on a per-project basis in a virtual environment.

Alternatively you can use pyenv. You can install it here: https://github.com/pyenv/pyenv-installer
Here I have more than 10 different versions of Python installed for testing purposes, and I can switch between them as I like:

Code: Select all

 ➜ pyenv versions
* system (set by /home/spamegg/.pyenv/version)
  2.7.18
  3.10-dev
  3.4.10
  3.5.10
  3.6.13
  3.7.10
  3.8.8
  3.8-dev
  3.9.2
  3.9-dev
  pypy2.7-6.0.0
  pypy2.7-7.3.1
  pypy3.5-7.0.0
  pypy3.7-7.3.3
User avatar
Luke Johnson
Level 1
Level 1
Posts: 38
Joined: Sat Jun 12, 2021 2:44 pm

Re: Using newest Python versions (Or: "When One plays with a Python, One usually gets bit...lol.")

Post by Luke Johnson »

spamegg wrote: Tue Jul 27, 2021 5:05 pm Alternatively you can use pyenv. You can install it here: https://github.com/pyenv/pyenv-installer
This looks interesting, although I will really have to work harder on understanding the installation process.

Will using pyenv affect performance, though?
I will be using a script I made a lot, but it takes a long time to complete. Sometimes I really need speed, even though that isn't really Python's selling point.

EDIT Found this in the Real Python article on pyenv:
pyenv inserts itself into your PATH and from your OS’s perspective is the executable that is getting called. If you want to see the actual path, you can run the following:

$ pyenv which python
/usr/bin/python
Isn't this path going to mess up the system-wide installation? Or the system-wide installation is in the python3 folder, so there is no problem?


EDIT 2: From the Arch Wiki:
To install the current release of Python 3, install the python package.
From the Arch packages listing:
python 3.9.6-1 Next generation of the python high-level scripting language 2021-07-22
So beautiful... I hoped it would work in Manjaro, too, but it doesn't seem to.
User avatar
spamegg
Level 14
Level 14
Posts: 5117
Joined: Mon Oct 28, 2019 2:34 am
Contact:

Re: Using newest Python versions (Or: "When One plays with a Python, One usually gets bit...lol.")

Post by spamegg »

No, pyenv just changes where the python command points to, temporarily. You can keep switching it back and forth. After you're done using a non-system version, use pyenv global system and everything is back to normal:

Code: Select all

 ➜ pyenv versions
* system (set by /home/spamegg/.pyenv/version)
  2.7.18
  3.10-dev
  3.4.10
  3.5.10
  3.6.13
  3.7.10
  3.8.8
  3.8-dev
  3.9.2
  3.9-dev
  pypy2.7-6.0.0
  pypy2.7-7.3.1
  pypy3.5-7.0.0
  pypy3.7-7.3.3

 ➜ pyenv which python
/usr/local/bin/python

 ➜ pyenv global 3.9.2

 ➜ pyenv which python
/home/spamegg/.pyenv/versions/3.9.2/bin/python

 ➜ pyenv global system

 ➜ pyenv which python
/usr/local/bin/python
For example, if I want to do testing on multiple python versions all at once, I do something like this:

Code: Select all

pyenv global 2.7.18 3.4.10 3.5.10 3.6.13 3.7.10 3.8.8 3.8-dev 3.9.2 3.9-dev pypy2.7-6.0.0 pypy2.7-7.3.1 pypy3.5-7.0.0 pypy3.7-7.3.3
Then I run my tests. Then I go back with pyenv global system.

Most of the time you won't even have to do this though. Your IDE like PyCharm or VSCode will detect your alternate versions installed by pyenv and you can switch by clicking the appropriate thing in the GUI.
User avatar
Luke Johnson
Level 1
Level 1
Posts: 38
Joined: Sat Jun 12, 2021 2:44 pm

Re: Using newest Python versions (Or: "When One plays with a Python, One usually gets bit...lol.")

Post by Luke Johnson »

Thanks a lot.

Found this, too, didn't try it out but it seems to be just the tutorial I need (pyenv + Atom): https://www.youtube.com/watch?v=oZe4-ROWO9E

Some kind people have recommended that I use EndeavourOS. EndeavourOS has the latest Python release. Very cool.
User avatar
Luke Johnson
Level 1
Level 1
Posts: 38
Joined: Sat Jun 12, 2021 2:44 pm

Re: Using newest Python versions (Or: "When One plays with a Python, One usually gets bit...lol.")

Post by Luke Johnson »

spamegg wrote: Tue Jul 27, 2021 5:05 pm You don't have to install Python 3.9 or 3.10 system-wide, in order to use them. You can use an IDE such as PyCharm that can install those on a per-project basis in a virtual environment.
I was able to install PyCharm, but it seems like it's impossible to install a new version of Python from within PyCharm. I have to install the new version somewhere in the system. But if I do that, what do I need PyCharm for? All the risk has already been taken.

Am I missing something?
When you configure a Python interpreter, you need to specify the path to the Python executable in your system. So, before configuring a Python interpreter, you need to ensure that you've downloaded Python and installed it in your system and you're aware of a path to it.
https://www.jetbrains.com/help/pycharm/ ... nterpreter

PyCharm sees the Python that came with the system and doesn't offer a new download.
User avatar
spamegg
Level 14
Level 14
Posts: 5117
Joined: Mon Oct 28, 2019 2:34 am
Contact:

Re: Using newest Python versions (Or: "When One plays with a Python, One usually gets bit...lol.")

Post by spamegg »

Sorry, my wording was ambiguous.
I meant that you
- first install different versions of Python with pyenv, and then you can
- select them in PyCharm easily without having to switch Python versions on the command line.
I meant to say that:
- using PyCharm, VS Code etc. is optional, but can make things easier.

I will make sure to break things down step by step and write out exactly what I mean from now on.
User avatar
Luke Johnson
Level 1
Level 1
Posts: 38
Joined: Sat Jun 12, 2021 2:44 pm

Re: Using newest Python versions (Or: "When One plays with a Python, One usually gets bit...lol.")

Post by Luke Johnson »

A quick update, might help someone later.

I was able to install pyenv. I recommend finding bashrc outside of the terminal, by navigating through the GUI (it's a hidden file) and opening it in a text editor via right click.

I installed PyCharm and was able to use the Python versions I installed. My test script wasn't slowed down at all. I think it's even slightly faster than in Win7 on the same hardware and in the same Python version.
Locked

Return to “Beginner Questions”