 Hello everybody. Do you hear me? It's the last talk here Hello, hope you're enjoying DevConf as I am. I'm here to Tell you something maybe interesting. Maybe not. We'll see about Python 3 and About the fact that you really should switch to Python 3 right now. It's too late actually, but right now It's still better than you know before Christmas So why I am I am a member of Python maintenance team in Red Hat I am people I organized people meetups in Ostrava. I'm head of by consistent conference organizers I'm teaching and organizing by Piley these lessons and courses in Ostrava and in a Private life, let's say I don't have much time for that, but I'm a drummer and also a volunteer firefighter What in Python maintenance team what are we work on? Actually, we take care about and we do Python. It's not that surprising, right? But actually we take care about CPython development as well a few members are core developers in a CPython interpreter We do take care about Python stacks in rel and Federa about some really important packages So if you're enjoying a lot of pythons in Federa for testing ready to use that's our work We also we also moving things forward if necessary So yeah, we are working right now on removing Python 2 from from next Federa And also we are ready to help upstream project and upstream developers So if your project need, you know some luck to support python 3 do not Wait and contact us especially me And I will help you to do it. I will help you crack the hard nuts What I work on my last big project was to prepare python 3 stack for rel 8 Because yeah next rel my release will be python 3 And I was working on the large projects which redhead use in our software Stacks, which needs to be ported or at least support python 3 Namely, it's a body. It's Federa update system. It was Samba I think that everybody knows Samba. It's a really large core base and for example Ansible Also a really big project based mainly on python So I was working still on by the maintenance but helps these upstream projects to support python 3 To welcome the future First question I have for you And I have you know some prizes How old is python? Where I mean where when uh the development of python language starts raise your hand, please Again, please 28 years ago Any other opinion It was 30 years ago But okay, you are close. Okay, and I you know, I cannot eat candy. So it's just It's actually the same date as the first web page Yeah, when I discovered the fact it was very surprising and there's a first Linux kernel and also visual basic Second one second can you how old is python 3? What's the release date of the first python 3 release? 2008 yes Please 2008 which is the same year as a github was you know published as a first mobile phone with android Yeah, it's really old And uh intel atom familiar processor was announced. So it's really long time ago And you may or may not know that python 3 is the first Backward incompatible release which means that if you write some software in python 1 or python 0.1 Which was released in 1991 Then it should work Should work in python 2 7 as well. So python 3 is the first backward incompatible release Another question But I am out of candy. Sorry, uh, please here's your raise your hand if you know the zen of python No, no remember just know that something like it exists A lot of people cool The zen of python is the 90 rules which every python developer should follow but not only python developer as you can see it's a general one not only for python If you don't know it just open Cli interactive session with your python python 2 and python 3 of course and type import this and enter and it will show Show up for you and there is one really important and my favorite one There should be one and preferably only one obvious way to do it in language in a python language or in your software in our api depends on you and Because you know from python 0.9 from 1991 to python 3 in 2008 a lot of stuff Accumulated in the language a lot of new features a lot of ways how to solve a simple problem Multiple ways and it was kind of mess and not all the ways was You know, optimal optimized for best performance, etc, etc, etc. So decision was made to Break the hardware compatibility to let the python again be Fit this all of these rules so that's the one reason and The second one is of course unicode Okay, that's the history forget about it Why should you care? Why should you care about python 3? the most Important thing you should know if you're still using python 2 is that python 2.7 end of life is really near The support will be dropped in the first january of 2020 if you want to Countdowners like I do just open the page python clock.org and you will see it's exactly 11 months and six days or seven days right now So yeah, the python 2.7 will be unsupported by upstream and as you can see in other talks today from for example victor's thinner Python core developers do a lot of work regularly so Who will do the same for you who will support the language who will fix all the cvs, etc It's open question, but still if you can you should drop it as well Linux distros linux distribution don't ship python 2.7 by default For a few years right now, for example, federal 23 in 2015 was without python 2 by default Ubuntu 2 years later Everything is going that way to You know keep you Aware that something is changing The most important libraries and projects support python 3 without any problem Actually, if you take a look on this page python 3 Readiness.org you will see that 359 out of 360 Most downloaded projects on pypi support python 3 The one which doesn't is apache beam I don't know why But if a lot of projects and a lot of huge code bases, I mean like numpy pandas scipy and all of this All of this stuff support python 3 why you should know List of projects with Will stop supporting python 3 you can find it On python3statement.org and I think that you will be surprised check this page. There is a interactive Chart when you will see when each project will stop support python 2 And I think that you will be surprised because for example pandas did it already numpy and scipy is on is in bug fixes only state And a lot of project will follow and a lot of project will do it before 2020 in this year for example in a half As Somebody told me the whole next Samba samba release will be the last with python 2 support and then the sub python 2 support there where we drop as well, so Be aware Python 3 python 3 means performance boost for you I don't have any exact Examples here, but if you Try to find something on youtube you will find talks from huge conferences about for example, look Big websites like instagram or facebook switching to python switching to python 3 and what does it mean for Their performance and yeah, it's performance boost. So it's a Good also for not for you, but also for your servers And python 3 contains useful new features If you want to support both you can if you want to use new features you can in python 3, you know You can drop python 2 support. I think as well. Why not? Okay, let's talk a little bit about strategies how to do it There are multiple ways how we can support python 3 the first one is Keep python 2 and python 3 cone based in separate branches, which means that you will have to do all the box fixes twice you will have to Do a review twice you will have to do everything twice, but Possibly make sense for your scenario depends on your project, but It's not the best way how to do it But in this case you will you can use all the new features in python 3. That's cool Use tools like 2 to 3 or 3 to 2 etc. It's actually not that good idea If your project is really small you can set up something like 2 to 3 or 3 to 2 in your continuous integration or Some other pipeline and it will convert your code dynamically for you Which means that it will still maintain python 2 when you do automatically transfer to python 3, but It doesn't work on a large code basis, of course And if your project is small why not use python 3 or And I think that you won't be surprised the best strategy to support python 3 is to write compatible code It's not that hard as it may look Because a lot of features from python 3 was actually backported to python 2 in some way To make this transition as simple as possible and we as a python maintenance team We are helping to do it even simpler and even easier for you So how to make my code compatible? How to do it what you will know what you will need to do it You will have to have good test coverage Test coverage is a very important thing if you want to change Basically the language you are using for your project. I know I know I know nobody wants to write tests, right? But there is I think First advantage to writing test is that if your test coverage is really good you can Give the task of transition to python 3 to some, you know Trained monkey to do it for you because all you need to do is make all tests passing in python 2 and python 3 and that's all And I Don't know if I want to say that but actually because samba ansible body and a lot of other tools Has a good good test coverage. I Don't really understand how it looks how it works in the background and I'm still able to port it to python 3 So if you have a good test coverage, it will take some time, but not that much You will need to make some decision, of course, because for example as We will discuss it later a little bit, but In python 2 was just wire variable with some Base string unique code whatever I think doesn't work just put there some decode in code decode and code and it will work So this is not true anymore. So you will have to decide whether this variable should contain unique code Or should it contain bytes etc. So it will need some decision probably from a people who understand the project and It will take some time some time Of course But as I said with test coverage you can do the works Some intern can do the work for you Okay, let's take a look on the common things you will have to deal with when supporting python 3 And because I said that the best strategy is support both in the same time Let's take a look how to do it how to support both pytons in the same time and We can take a look also why these things Had to be changed let's start with Some thing really simple which all of us know for example print print was a statement keyword in python 2 And print is a function in python 3 Actually, you can use print as a function also in python 2 if you from python 2.6 If you use a future import, so it means from future import print function And ta-da python 2 has also print as a function You may wonder why the hell something that simple and it need to be changed really from statement to function Why what the benefit of this? let me show What if you want to use more complex print like this one? Oh, it's ugly Right or not I think so and The same print there is only one difference And I want to know What the difference between two the prints does? Who knows it? Maybe I find one more candy. Ah, yeah, it's here so Yes, right the second print contains Commas at the end of the line And the result is that it don't Put the new line in the end of the string hello world printed on standard error output Oh, that's not look good, right? So if the things Go more complex It makes sense to make easier and more understandable by readers and more readable So in python 3 print is just a function It's simple and it has a similar api with a rest of the python ecosystem and it's Far it's more more more readable than python 2 version I can only imagine that I will Teach pie ladies on courses how to do a print without new line It's impossible, but in python 3 it's perfect handling exception If you have only one exception, let's say zero division error Everything is fine and you don't want to store it in a variable. Let's do it this way Of course, you cannot divide by zero, right? Only Chuck Norris can do it But what if you want to catch two exceptions? I remember that there was some comma somewhere. Let's do it this way Will it work in python 2? No, what what the second one do? Sorry, don't be scared. I'm that bite Missing your brackets and what this will do without brackets No, it will work Yeah, yeah, definitely because there's no brackets here It will store if it catch the zero division error It will store the exception in variable with this name Oh, no The right way how to Do the same in python 2 is using brackets around a list of exceptions And another comma and then the name of the variable you want to store exception in Oh, come on. It's ugly and Everybody can write it in a bad way. So python 3 introduced new keyboard here s which is Good separator between The list of exceptions and the name of the variable you want to store exception in so it's more readable. It's less, you know Ready for mistakes, etc another thing Another thing actually you don't want to change anything to Benefit from it is exception chains. Imagine you have this simple call You want to read some config file from from disk and you want to catch exception Race it by python io error if the file is not found and you want to raise your own exception For example config not found error With python 2 you get only this trace back This short config not power error, but in python 3 because the exceptions tech was redesigned And it store exception in a separated objects You will have this very useful trace back which will help you a lot Especially when porting because you know when porting the things may go really really wrong So you can benefit from the information About all exceptions rise it from the back to the top So this is This is a really cool new feature. You don't have to do anything special to benefit from it Okay, next one sorting using cmp in python 2 python 2 has a You can you know define some function which will Work as a comparator Function of two values and you can use this you can use this function as a keyword argument for sorted function it works it has Visible disadvantage because the compare function has to be called Every time you want to compare two objects two values So it it's yeah, it's disadvantage of this of this approach and In python 2 and in python 3 of course you can use a key a key keyword argument for sorted Which will also use the function defined by you or a lambda if you want But with the key The function you define Has to be called only once for every value not every time you want to compare two values So for example for sorting list this approach is much faster And again, if you remember the zen of the python You remember the One rule. I like the most Now it's there is no one way and not neither one obvious way how to do it The same thing applies if you want to implement comparison for your objects And again, you can use the cmp cmp special method for your classes But in python 2.1 I'm not kidding in python 2.1 was implemented the rich comparison Which means that you can compare in a lot of ways you don't have to because in the previous example Here you always have to implement the full comparison Comparism which will return every Which will return a value representing all the comparison situation But with the rich comparison you can choose only the one you want to implement Lower than greater than equality, etc. Etc. And again, we have multiple ways how to do and how to solve Kind of simple problem. So in python 3 the cmp the less optimized Way was removed Very common thing you have to deal with Imports in python 2 Uh Python 2 prefers local files instead of the globals, which is a problem because if you want to if you have a large core base It's a higher probability that you will name one of your files the same like The name of something in the standard library and python 2 will prefer the local copies the local files Instead of the global python standard library from python 2.5 You can you can use the dot in imports to make it relative explicitly But in python 3 you have to use the dot to make it to make import be relative Which means In python 3 if you write some import It's always the global one if you want to import something in the folder You are or in your package structure. You have to use the dot in imports if you want to if you want to Standardize the approach and also make the code compatible for 2 and 3 python You can use the future import from future import absolute imports and this will make all Imports by default absolute and if you want a relative one you can use the dot Again very simple fix To make it work in python 2 and python 3 Data formats. Let's talk about it a little bit What is data format data format is a way how you Take any information like images like music Anything you want it's a way how to store it on disk like bytes But for images and for music, we know it, right? We have a lot of photos and mp3s, but what about text? What is text? This is duty of aid eski or something else You can pretend that text is only a sequence of ascii hearts and store it at bytes And then you meet franticic brzezina and your application will crash With a huge unicode the code error and you will try to place something decode in code. Ah, and still doesn't work Doesn't work like that and also cannot ignore images. Sorry, but you cannot because it will make your users sad emojis, you know rule the world and python upstream developers knows it so after a lot of years They decided that python 3 should contain a separated types for text which is in python 3 unicode by default and bytes Which is bytes nothing more plain text is a myth So python 3 has a set string set error type for text which is unicode and bytes for binary data And this is the I think maybe the hardest Think to port because you will have to make a decision Whether you want to store text or bytes and for example if somebody is porting the code for you It might be even harder because I don't know what this variable contains I don't I cannot make a decision for you. So this is if you will have for example some intern or some student of work for you and port python to python 3 compatible form This is up to you not up to any trend monkey You will have to decide this is the decision. I was talking about Changes in the standard library standard library or again Contains a lot of changes For example some renames built-ins and range x range and reduce move to func to modules Module etc row input. It's not row input anymore. It's just input Etc etc It's not that big deal. Maybe it is for you But I'm not taking it as this because there is a lot of modules which will help you to build the bridge between python 2 and python 3 and you don't Need to take care about these simple changes anymore. I will show it to you later Python c extensions. Is there anybody who need to write python c extensions? No Okay, you Of course If you need to write python c extensions, you're gonna you're gonna have a bad time Honestly, because python upstream developers said we are trying to develop language Which will be you know easy to write easy to read easy to beginners Etc etc. Why would somebody? Write python extensions in c. We are making python For easy development. Why you see to make python extensions? So My advice is if you Really need to do it. You're gonna have a bad time because python developers Say Nobody should do this So we are not keeping backwards compatibility in any way and everything change it but Help is on way So we have solution also for this. So if you Want to write c extensions? I will show you how to do it But if you can choose, please use siten or cffi and c types siten is uh, you know Modified python which can be easily translated to c code and it makes basically c extension for you and for Basically, no price and cffi or c types are modules for calling Existing c functions from python. So if you can choose, please do not write c extensions directly using python capi Use siten or cffi or c types But if you have to I will show you how I will show you what to use Help is on the way as I said If you are a really conservative guy, if you Do you really think it's a right time to support python 3? Really, it's only 11 years. I'm not sure whether it's stable or not If you are this type of conservative guy, we wrote Conservative parting guide for you. You will find it on the readie docs and it's the Complete list of the things you will have to change in your code to support python 3 of course and What to do it? What is prevalence of the changes? Why this was changed in the python from time to time? And And also how to do it because of course, nobody wants to do it manually. So there is uh, help How to use tool called python modernize? What you know options what arguments to use that only the one thing is changing In the time So it's really conservative and it's really here to you What you need to do to support python python 3 in your code base and also python 2.7 And if you be if you will be careful also python 2.6 Come on the sixth library It was mentioned before why it's called six becomes two times three six, right? Six contains It's a wrapper and contains some utilities to help you building the bridge between python 2 and python 3 Which means for example, if you want to import something which was renamed in python 3 You can import in from the six and it will Do basically the work for you So if you run the code in python 2 it will import the in the python 2 name If you work the if you want if you run the code in python 3 it will import it under the python 3 name, etc, etc also Contains a lot of stuff which help you with unicode, etc. So six library It's also in the conservative pointing guide. So Yeah, and it really works and it'll really help you trust me This is a project which helps basically the one from you who need to Who need to write a python c extensions? It's something like six it contains a lot of macros for c And it'll help you to import python c extensions as well And the last but not least Wait for it. It's me Don't hesitate to write me to call me. I am a firefighter. So I'll pick your phone even on a midnight You know, it's not a problem at all My girlfriend hates me because of that, but it's not a problem at all Don't hesitate call me Send me an email. I will help you. I will help to I don't know set some pipeline for a project to support python 3 in the right time, which is uh now Thank you for your attention We have plenty of time for questions so any questions You can also catch me on the whole for example, if you Yeah No That's a question. I heard a little. Yeah, I repeat the question. Yeah, sorry The question was if there will be some new breakages some backward incompatibilities in next titans No, hopefully not But we may Release python 4, but it will keep backward compatibility. So Don't worry nobody even the python upstream developers and me neither Nobody wants to repeat the same process again. We are fading with we are fighting with this for 10 years So we don't want to do it anymore If your tests are also in python 2, uh, yeah, the test want to do If your test coverage is good, but your tests are also in python in python 2 question so The best approach to port basically if you have some test or if you have For example, good test coverage is to set up some tool. For example talks Talks or you may know traves, etc It's a tool which can run your test on in multiple in multiple, uh, languages in the same time So you can pick the versions of python you want to support for example 2.67334567, etc, etc And then you then you have only one task to solve make all green That's all sounds simple. I know but if you test are in python 2 and it's uh, it's Yeah, it's common scenario Then you will have to fix your test first Before you can, you know, hire a trained monkey But it's not that big deal Usually because how many, you know Features you are using in tests It's basically call the function and compare the results and call the function and download something from web and compare the results So it's not that big deal in test basically, but yeah, of course If you want to store unicode somewhere you will accept unicode in test. So it Needs some work to be done any more questions The question is whether consensus is that it was a bad idea to break the bad word compatibility Uh, it wasn't The consensus is that it wasn't a bad idea to break it because You know We will have to do it sooner or later So it's and everything was prepared and the first plan was okay Just drop python 2 at all and make only python 3. It's cool. And then somebody's scared. Are you crazy? Do you know how many lines of code are written in python 2? It's no no idea and then uh, the deadline The aol for python 2 was postponed and postponed and postponed and postponed. So nobody Actually, except that will be That big deal to transform and that's a good lesson we learned. So we don't want to do it anymore so yeah, but it We will have to be done sooner or later still so Job is done basically Question is is there any chance that the deadline of uh, python 2 ll in 2020 will be postponed again? No, there is no chance To python 2 to be the deadline for from uh, the upstream developers to be postponed Of course, there are projects like I don't know a Tauton is basically python 2 forked from upstream with some backported features from python 3. So it will probably Be somebody who will take care Of python 2, but it's a lot of work really So I don't think the upstream developers don't want to do it anymore Actually, the first announced deadline was 2020 and then people start asking like in december 2020 No first january 2020 and that's all no more discussing So no python upstream developers don't want to do it I don't think anyone want to do it No, there's I think that there's no chance It's We are too close to remove python 2 from everywhere We don't want to postpone it and you know, we want to party next year on picon so Yeah, yeah, but on picon us there will be a huge party for removing python 2 So we don't want to post, you know, who wants to postpone party? Okay Any more any more questions major Is there any major libraries which don't be poured to python 3 or at least to supper python 3? I would say no But depends how, you know, where your dependencies is There are some of course, but for every Important library if the upstream developers don't want to supper python 3 There is a fork which supports python 3. So it may if you're using some, you know, not that common Libraries it will need maybe to change your dependencies and to add up to the new one But as I said before on the pipi the 300 and 360 most downloaded libraries is done are done And you can also check it on python 3 reading is there is so huge list So it's not a problem Any more questions? Yeah, as I said before there are a lot of project already dropping or already we've already dropped python 2 support So you might be really surprised when you do update of your software stack and if This presentation would be really boring for you And it's evening. I know I can understand but please remember one thing If you start new project or if you actively work on some project Move it or start it with python 3 Because if you don't after 10 years from now somebody like me Will need to do a work for you and I don't want to Okay, so please every new project starting python 3.7 school full of new features Every program you need to actively maintain python 3 6 python 3 7 depends on what you want, but Move it. It's not that bad somebody and it's a huge project. So you can too trust me Thank you very much