r/learnpython • • Jun 08 '21

TIL Python 3.9 is the end of Python 2 compatibility. Time to upgrade any old code to Python 3

Python's been great about supporting Python 2 code in Python 3. But it looks like that's about to be over.

What’s New In Python 3.9 — Python 3.9.5 documentation says:

Python 3.9 is the last version providing those Python 2 backward compatibility layers

To check your code, run python3 with '-W error'.

python3 -W error myprogram.py

This will only check the code that runs. So it's best to run a good test suite.

The python devs have done a great job of managing compatibility.

493 Upvotes

46 comments sorted by

5

u/PatGrat Jun 09 '21

Newbie here. What exactly makes python 1/2 no longer work if they worked at the time they were written? Are the libraries changed enough so when it calls the code back it's an invalid expression now?

7

u/denova-trader Jun 09 '21

Yes, the libs have changed.

And python3 has quietly supported some of the old python2 libs for a long time. Apparently after 3.9.5 that support for python2 libs will be gone.

0

u/[deleted] Jun 09 '21

[deleted]

3

u/PatGrat Jun 09 '21

Makes sense, thanks for responding!

68

u/dogs_drink_coffee Jun 09 '21

I program some basic Python for years and I honestly didn't even know there was Python 2 compability lol

45

u/Akliph Jun 09 '21

Yo when are operating systems like Debian and Ubuntu gonna come with python 3 instead of 2.7 because that's the biggest pain in the ass

8

u/Username_RANDINT def get_random_int(): return "RANDINT" # TODO Jun 09 '21

What old version of Ubuntu are you using?

6

u/Akliph Jun 09 '21

I normally use Rasbian which is based on Debian (idk which version) but I always have to manually install python 3 and pip 3.

6

u/probablynotmine Jun 09 '21

Debian is well known to be stable, so I guess it will catch up slowly and steadily

5

u/Username_RANDINT def get_random_int(): return "RANDINT" # TODO Jun 09 '21

That's not the one you mentioned first. But as long as it's in the repositories there's no real problem, it's not like you have to compile it, it's just one command. How many times do you do a clean install anyway?

1

u/Akliph Jun 09 '21

I clean install whenever I switch my RPIs up for different projects and I usually have to ssh into each one to install python 3 and pip, it's pretty time consuming ngl

3

u/ThellraAK Jun 09 '21

cloud-init is also pretty good at doing this.

0

u/[deleted] Jun 09 '21

[deleted]

1

u/Akliph Jun 09 '21

Bruh what

5

u/paradigmx Jun 09 '21

I just want them to stop using python3 and pip3 and just make everything python and pip instead. I get compatibity issues and crap but I'd just rather not run any python 2 code at all anymore. It's been obsolete for years.

0

u/wyoming_eighties Jun 09 '21

this is not hard to configure yourself

4

u/paradigmx Jun 09 '21

I shouldn't have to at this point. Python3 came out more than a decade ago, it should just be the default.

1

u/Vaphell Jun 09 '21

it's the default in most distros already, but that doesn't mean that the name python is up for grabs no questions asked.
And python2 was EOLed just a year ago, and there are quite a few LTS installations in the wild that will have it for years to come.

You will be waiting for a long time. It's simply easier to forget that ambiguous python exists, and always go with explicit python3

4

u/[deleted] Jun 09 '21

Latest versions already do

3

u/platypusPerry245 Jun 09 '21

Ubuntu 20.04 already comes with 3, there is no python 2 out of the box

1

u/midnitte Jun 09 '21

Considering the same thing for MacOS, should you even use the included version? Seems like you could break a lot of things if you upgrade packages...

6

u/Yojihito Jun 09 '21

MacOS Python version is outdated crap for internal stuff. Never use that version.

Install Python 3.9 / 3.10 and just use that.

0

u/wyoming_eighties Jun 09 '21

Ubuntu does not come with any Python at all.

I think one point of confusion is that some systems ship with both Python 2.7 and Python 3, but python runs 2.7 and python3 runs 3, thus people used to just using the python command for everything end up getting 2.7 without realizing that Python 3 is even installed (just need to call python3 instead)

1

u/Akliph Jun 09 '21

Yeah should have mentioned this, I just feel like python 2 is so obsolete that the python keyword should just reference python 3.

1

u/shiftybyte Jun 09 '21

What am i missing here? wasn't python2 code already not running in any python3 without modifications?

1

u/denova-trader Jun 09 '21

You're right, shiftybyte, that a lot of python 2 code already doesn't work in python 3.

But even after you upgrade strings, etc., there are still a lot of libs that silently support python 2 deprecated behavior. It looks like the code that allows that behavior is going away.

For example, try this in python3, with and without '-W error':

import collections

try:
    # collections.Mapping is deprecated
    deprecated_class = collections.Mapping
except DeprecationWarning:
    raise
else:
    print('This just prints a warning.')
    print('The code still works until 3.10 (extended from 3.8)')

Referencing 'collections.Mapping' works works with a DeprecationWarning. Those warnings are easy to overlook and after 3.9.5, the warning turns into an error.

1

u/sqjoatmon silentquasar Jun 09 '21

That totally depends on whether it was written to be compatible with both.

1

u/shiftybyte Jun 09 '21

And after this update it can no longer be written to be compatible with both?

1

u/sqjoatmon silentquasar Jun 09 '21

I guess go to op's link. I doubt u"unicode string" is going away

3

u/dewilso4 Jun 09 '21

Can I upvote for the info but downvote for the message? :(

5

u/Extraltodeus Jun 09 '21

Calibre dev is gonna cry

19

u/ASIC_SP learnbyexample Jun 09 '21

They are already using Python 3

New in calibre 5.0

Welcome back, calibre users. It has been a year since calibre 4.0. The two headline features are Highlighting support in the calibre E-book viewer and that calibre has now moved to Python 3.

4

u/Extraltodeus Jun 09 '21

Oh I didn't know

1

u/Jerome_Eugene_Morrow Jun 09 '21

If I remember right it was a pretty fraught process that almost resulted in a project fork. Huge claps for them for getting everything into Python3 though. Once it started moving it moved fast.

2

u/wyoming_eighties Jun 09 '21

Its not a big deal because you can still install old versions of Python very easily; https://repo.anaconda.com/miniconda/

2

u/ivanoski-007 Jun 09 '21

good, too many people won't let go of python 2 for some reason, I even saw a user here learning python, through python 2 even though everyone highly recommended not to do so, some people honestly

0

u/[deleted] Jun 09 '21

[deleted]

10

u/PM_ME_YOUR_DOOTFILES My advice is not under warranty. Jun 09 '21

The second article is great and demonstrates how small looking decisions can have massive ripple affects across a large code base.

5

u/zurtex Jun 09 '21

The original intent of Python 3 was not to be compatible in any way at all, you would just rewrite all your code from scratch understanding all the differences that has occurred.

This was a naive approach and I've read many interviews of core devs appreciating this. Which is why you can see from Python 3.3 / 2.7 onward a concerted effort to bridge the gap, but really the damage had already been done and it was going to take many years to fix.

That said I think Mercurial is an extreme example. I've not read that specific blog post but I have been aware of some of their problematic requirements which were 1) Requiring low latency start-up time due to massively multi-process architecture, and 2) Direct binary access to file system APIs.

Neither of these 2 requirements have been well suited by Python 3, in fact neither of them were well suited by Python 2 either but were just easier to hack in to working. I hate to say it but I just don't think their approach was ever a good fit for Python, if I had either of those 2 requirements I would probably pick another language. That said I hope in the future Python fulfill either of those 2 requirements.

4

u/wyoming_eighties Jun 09 '21

However, the approach of assuming the world is Unicode is flat out wrong and has significant implications for systems level applications (like version control tools).

Really, this just goes back to my very first impression with the article; Why the hell were they writing a systems level tool like Mercurial in Python in the first place?? This really seems more like a case of choosing the wrong tool for your project, then doubling-down on it instead of just using something that works better

0

u/[deleted] Jun 09 '21

[deleted]

1

u/wyoming_eighties Jun 09 '21

seeing how the article even mentions launching the same month as git and git is made in C (? https://github.com/git/git ) I am gonna have to say whatever git was using

1

u/Vaphell Jun 09 '21

yeah, because competent C programmers grow on trees in great numbers. I know my way around programming in general and have some experience with C, but I ain't no Linus and life's too short to deal with all the footguns C has in its toolbox.

1

u/wyoming_eighties Jun 09 '21

I mean, my response to this would be that they should not have even started the project if they did not have competent people who could do it in the best language.

I know people that write everything in Python simply because that is all they know and those people's systems-level projects are attrocious. This situation with Mercurial does not sound dissimilar

1

u/Vaphell Jun 10 '21 edited Jun 10 '21

I mean, my response to this would be that they should not have even started the project if they did not have competent people who could do it in the best language.

If that were the go-to approach, we'd have 90% fewer programmers and 99% less software. Let's be honest, there are millions of decent programmers who can whip up good software in the language of choice, who would get destroyed by the discipline and the low level grind the C language demands or else.
Anyway 'best language' is a fallacy. In the real world language needs to be good enough and the choice takes into the account the wider ecosystem, speed of development, size of the talent pool, etc.
There is a reason why Java continues to be one of the top languages despite being a mediocre ball of boilerplate. Schools churn out java coders by the thousands and the IDEs for it are fucking amazing.

Is there something in version control software that necessitates that the whole project needs to be in C? Is the compromise between the development speed and raw performance worth it? I could see some tight bit mangling module to be worth a rewrite using C hooks, but the whole thing? I am not convinced.
Git is written in C, because Linus who had an itch to scratch breathes it.

2

u/sqjoatmon silentquasar Jun 09 '21

I don't have sources, but I'm petty sure Guido et al are on the record saying they know they didn't handle the transition well and regret that.

3

u/[deleted] Jun 09 '21

What could Python do better? It isn't really a language's fault that some of it's users are unwilling to embrace changes.

17

u/badge (ish) Jun 09 '21

There’re a whole host of examples in the second of OP’s linked articles. E.g. removing the u string prefix from Python 3.0–3.2 made it impossible to write compatible code, and bytes formatting was removed and only reintroduced in 3.5, 7 years after the initial release of Python 3.0.