Python 2to3 - Convert your Python 2 to Python 3 automatically!
Skills:
Tool Use & Function Calling90%
Key Takeaways
The video demonstrates how to use the 2to3 module in Python 3 to convert Python 2 scripts to Python 3 automatically, covering the conversion process, tool usage, and practical steps.
Full Transcript
hey guys what is going on welcome to another Python tutorial video this video we're going to be talking about how we can convert python 2 scripts to Python 3 scripts I had no idea about this I actually just kind of stumbled on it uh not too long ago but there's actually built-in module that comes with Python 3 uh installations and it's called 2 to3 and what it's going to do is it's going to run through your python 2 script and convert it to Python 3 and I've used this on extremely large scripts and it I'm trying to think of the only thing the only thing it didn't do very well on was the hmac module which is for like encryption and stuff so if you're not using that then you're probably more than fine but uh I have been really impressed with the converting ability of two to three I mean it it does it well so anyway I'm going to show you guys how to use it um first I'll show you guys on Windows I think most people that watch my videos are actually using Windows U but I will show you on a Linux machine as well just in case uh someone is curious uh how it works but anyway we have um URL lib 2 and we've got a bunch of Python 2.7 code going on here um obviously this won't run in Python 3 and we've got you know first of all there is no URL lib 2 in Python 3 this isn't the proper syntax in three uh you can't do this print function in three this is improper Syntax for a try and accept it should be accept exception comma e and again you can't print print like this it's a print function so um I try to throw in a healthy uh short enough script that everybody could uh test it while at the same time um not too long it'd be crazy so if you happen to have your own python 2 script use it or if you want pause the video type out this script um or just type out a print high or something like that just to get a feel for it so anyways uh let's do it so the name of this file I have placed these files on my desktop I've got two to3 me and then within it we've got python 2 ugliness and that is this file that we're looking at right here um so to use uh two to three depending on what machine you're on you might have to type things differently um but uh just hang hang in there and uh I'll try to hold your hand so we'll close out of this and what you'll do to open up a command loine to whatever folder you're in just like go to the folder hold shift right click open command window here if you're on Windows and now you're ready to type in the command and the command for 2: three is going to be the following first you have to reference python if you're on Windows and python is in your path as in Python 34 and not python 27 or whatever version of two you might have um although I'm not quite sure why you be running uh this if you were not using Python 3 but sometimes python 2 is in your path before Python 3 and it uses that so if this does not work for example you should be able to type python hit enter and it should be Python 3.4 three and some version of three if it's not you better type the full path if you can't type this and uh you get like an error Python's not recognized then you need to type the full path to python well I cannot get out of this interesting I thought a control C or control D would do it anyway if you got out or if you typed Python and you're stuck uh open it up again um otherwise to reference python give the full path so c/ python 34 SL Python and actually I think the p in Python is capitalized yeah so it's actually capital P python but then the other one will be lowercase so python 34 python you might also want to reference it as python exe or just straight up python it doesn't matter anyway here's the syntax so first what you can do is you can just say Python and then you sometimes you might be able to get away with with uh like 2 to3 and then python to ugliness. Pi something like that um right but it's saying that it doesn't exist on Linux you you again might be able to get away with that but for most Windows users you'll have to type this so Python and you might have to specify the full path of python then you specify the path to two to three the path is going to be cpython 34 uh slash tools slash um and actually tools is a capital T tools and then slash scripts slash 223 uhp okay so you're calling that script and then now we're going to pass um as a sort of parameter to that script if you remember uh I showed you guys a long time ago how you could pass parameters to Python scripts through the command line that's what's about to happen here we're going to pass a parameter through um and the parameter is going to be the script itself so python 2 ugliness. p now hit enter hopefully this will run I was long to type okay so here it basically tells us everything that we want to we would might want to change about the script so here it you know suggests renaming which is the exact same name then it suggests well we would get rid of url2 replace that with import url. request URL Li error and URL li. parse try we would retain but we would get rid of uh this URL lib 2. URL open replace it with URL li. request.url open so on we would get rid of print X and use the proper and then we would get rid of um accept exception did I I think I'm must have uh mistyped my version of exception let me go back to that script real quick accept exception as e um sometimes I guess I confus myself but except exception comma e is the Python 2.7 syntax sorry about that uh we'll replace that let me draw that one more time let's just run that again um so here you can see that it it C catches the error right except exception comma e needs to be replaced with accept exception as e print string and then the function for the print and all of that so then it also says what you need to modify now that's all fine and dandy but it didn't actually mod Mod ifier script so how might we actually get it to modify the script uh we can just throw in a dash uh a-w basically um so we'll just hit the up Arrow here and where we call the script we're going to pass one more command but before the actual script that we call right so after 2 to 3. Pi do space dasw that's it and that's for write and basically it's going to change our file and it's going to write to our file um itself so it does that now we'll go to um that folder here 2 to3 me and you should have a backup soback just means backup so this is the old file right the old one that was replaced and then we have the new one that was just written so I'll edit that and you see it has the new code to it and this is now Python 3 code so hopefully we can save and run that and uh and sure enough it does indeed print out uh the code so uh that's it with two to three now we'll just show you guys in Linux as well but the Linux will be basically the exact same thing you're going to just reference um you know 2 to three so you might have to give the full path to two to three but in many cases you can just say 2 to three so anyways if you're on Windows you can pretty much stop here if you're on Linux I will just show it anyways so uh let's do that now all right so in Linux uh here I'm just on a Raspberry Pi so it's UB to um let's go ahead and CD into uh desktop apparently I don't know we might be there uh yeah why can't we there we go um and now let's just do pseudo Nano um pi2 script. p and I just copy and pasted or copied that uh other script that we had and import URL to okay so obviously our uh indents didn't stick try uh that TR statement can stay print X except exception e okay so that should work uh we can exit there save yes um so here I have two versions of python so we've got python installed so we can run um basically 2.7.3 alternatively uh we can also run Python 3 like that so we have both versions of python here so I could run python um and then what's the name of that script I think it was like Pi 2 something right so we can run that um and unindent a this is like some stupid indent issues hold on let's go back uh Nano oh maybe it's the exception that causes caused that one hopefully that's it let's try this one more time and let's just check it again okay okay so that printed it out so obviously it's working in Python 2 but Python 3 uh Pi 2 pi 2 script isn't going to run right it's syntax error so now what you can do on most devices you'll be able to just say 2 2 three like that and then you could say uh 2:3 Pi 2 script let's see if we can get away with it and right so it's just going to go straight into it and uh take its time apparently sorry this is on a pi and the pi is actually working right now yes that's why the codee's going so slow but anyway um so you can do that and then also obviously you can write so 2 to three-r um Pi script.py um so wait for it Go Little P go Little P that could as I like to say okay so it modified it um again let's do LS here and you can see here is our backup so it's still there um and now we can do Python 3 Pi 2 script.py run that and as you can see it worked so anyways um that's it just wanted to show you Pi 2 or 2 to three again I I had no idea this even existed so congratulations if you knew um but I just happened to stumble on it and I wish that uh someone had pointed this out to me a long time ago I've spent many hours converting scripts and this could have been much easier so anyway hopefully you guys enjoyed if you have any questions or comments leave them below as always thanks for watching thanks for all the support and subscriptions and until next time
Original Description
Python 3 Programming tutorial Playlist: http://www.youtube.com/watch?v=oVp1vrfL_w4&feature=share&list=PLQVvvaa0QuDe8XSftW-RAxdo6OmaeL85M
Sample code for this basics series: http://pythonprogramming.net/beginner-python-programming-tutorials/
In this Python 3 programming tutorial, we cover how to use the built in module called 2to3, which is used to convert python 2 scripts to python 3 scripts.
I had no idea this existed, and it has already saved me a ton of time. I hope it can help you as well!
http://seaofbtc.com
http://sentdex.com
http://hkinsley.com
https://twitter.com/sentdex
Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: Tool Use & Function Calling
View skill →
🎓
Tutor Explanation
DeepCamp AI