I am pretty much brand new to Python. I'm cramming this week trying to learn it and I've been handed some code samples to try them out and familiarize myself with them. Problem is that they were written years ago in Python 2. It's my own C++ code that I gave a guy years ago who converted them to Python 2 when he was learning both languages. Now I'M learning Python. I've only used Python 3 and like I said, I'm at the Newbie end of the learning curve. My question is rather generic. One, is there a way to run Python 2 code on my Python 3 installation without porting it line by line (i.e. can I simply put some line at the top of this old code and tell Python to interpret it using Python 2 rather than Python 3)? I assume that porting it from Python 2 to Python 3 will be quite difficult for me to do since, as mentioned, I'm a Python noob. Two, is Python 2 worth bothering to learn or is it obsolete? If not obsolete, should I learn Python 3 first, then Python 2, or vice versa, or learn them concurrently? And is Python 3 like C++11 (i.e. just adding on options that you can use, but all the old code should work when using the new C++11 compiler option. That one I already know the answer to. No. For example, print "Hello World" does not work in Python 3 (need parentheses in Python 3), but I don't know why (again, noob).

Again, just looking for general advice here. I have a week to learn Python and take a test for a job, so I don't have a whole lot of time to waste and want an efficient learning curve. There are lots of code samples out there. I just wanted to try this particular code out since I was the original author of it in C++ and my friend ported it to Python 2, so it would be interesting to compare/contrast, but not critical and probably not worth it if it's too labor-intensive. Thanks.

9 Months
Discussion Span
Last Post by AssertNull

Wasn't Python 2.0 released in 2000? 3.0 in 2008? That's sufficient time IMO to ignore older versions and unless you are paid to do something on 2.x, I'd go with 3.

Since it's a interpreter and a quick look seems to find what differs I'd move again to 3.5.x (latest.)

Edited by rproffitt


3.0 in 2008?

Wow. You're right! Just looked it up. Didn't realize that. He, for whatever reason, apparently was using Python 2.x in about 2012, so I figured it must have been much more recent! I'm using 3.5.1. Might as well stick with that. However, people still seem to be using the 2.7.x releases. They released version 2.7.11 in December 2015, a full seven years after 3.0 was released, so people must not be abandoning Python 2.x yet.


In fact, just a few weeks ago 2.x.x got a security update. You'll want to have that if you stick with 2.x.x. But a quick read of the differences don't seem so bad. Most of what folk do, from what I've seen won't matter.

Here's my view or method. When I start a new project I step forward my dev tools at that time. Then I stick with that unless there's a burning reason to change (or there's $$,$$$+ at stake.)


Among the various reasons why some people stick with python 2 is the fact that many linux distributions still come with python 2 as their default python interpreter, because many of their system scripts are written in python 2. For example, I'm using kubuntu 14.04, so April 2014, and my default python is 2.7.6.

As a newbie, you would waste a lot of time learning python 2 instead of python 3, so don't do it. On the other hand, python 3 is not very different from python 2. Apart from the print with parenthesis which you mentioned (print was a special statement in python 2 and became a regular function in python 3), the main change is the proper and transparent handling of unicode strings. In python 2, a string such as 'hello world' is a byte string, while in python 3 it is a unicode string. So if you work with python 2, there is a risk that you waste time struggling with conversions between bytes and unicode, and encoding issues. These issues are not very difficult to solve - for a python expert - . The other significant change is the renaming of some library modules. For example Tkinter becomes tkinter.

You could try the 2to3 tool which comes with your python installation, which is an automated script purporting to translate python 2 scripts into python 3 scripts. Here is a video Click Here of a guy using this tool to translate a program.

Edited by Gribouillis

Votes + Comments
2to3 times a better answer as well!

The Python Debugger for NetBeans appears to be written in Python 2. I tried the 2to3 tool. Seemed to work, mostly, but still apparently has some problems, particularly the "bytes" versus "string" difference in Python 3, which I guess wasn't a problem in Python 2. Looked up a few ways to convert a string to bytes in the debugger code, but I think since I'm a noob, I won't mess with it too much now and just learn Python without a debugger. The NetBeans 8.02 Python debugger seems to have been written about 10 years ago and their "Hello World" program in Python 3.5 has print without the parentheses, so it immediately crashes, so someone hasn't updated NetBeans' Python code for a while it seems.

Anyway, will stick with 3.5. Thanks.


A crash course for unicode from, to bytes (works in 2 and 3, only the 'str' class in 3 is named 'unicode' in 2, and the 'byte' class in 3 is named 'str' in 2)

ustuff = bstuff.decode('utf8')
bstuff = ustuff.encode('utf8')

Of course, you may encounter other encodings than utf8.

The main debugging tool in python is to read attentively the exception traceback that python sends to your console when there is an error. Also don't unnecessarily catch exceptions: they carry useful debugging information.

I often use a modified print function, which I posted here Click Here

Last advice: Always declare the source file encoding by writing for example

# -*-coding: utf8-*-

as the first or second line of a program, and if you write new python 2 code yourself, add this as the first statement of the program

from __future__ import (absolute_import, division,
                        print_function, unicode_literals)

This minimizes the difference in syntax between 2 and 3. With this, python 2 code needs print with parentheses for example and "hello world" is a unicode string. The statement is a no-op in python 3
Edit: typo

Edited by Gribouillis


As always Gribouillis, great advice. Thank you. I'm switching from using my friend's Python code as a model to your code snippets. I'm getting a little suspicious that learning from his Python code might be like cheating in class off the wrong guy. I keep looking at his code and thinking, "Wow, learning Python's gonna be easy, this is just like C++ code!", then it's dawning on me that perhaps it IS C++ code and I've been attributing it not working in Python 3 to incompatibilities between Python 2 and Python 3 when in fact it might just be bad Python code to begin with.

Anyway, I know you said "Last advice", but if you'll indulge me one more time I promise I won't bug you anymore in this thread. :) As mentioned, the NetBeans Python debugger appears to have been written in Python 2, and ten years ago. Can you recommend an IDE with an up to date Python debugger using Python 3? It seems odd that with so many people programming in Pythin these days and NetBeans being one of the most popular IDEs, that it isn't bundled with an up-to-date Python 3 debugger. Surely one exists?


I don't think studying my code snippets is good way to learn python. Most of them deal with specialized tips and tricks. More seriously, you could visit the hitchiker's guide to python (among thousands of other sites) to start with.

Using a debugger is not so crucial in python as it is in C++ for example, because there are very few mysterious bugs. Most errors are very easily detected by reading the traceback or printing a few variables.

I don't like python IDEs in general. I work more efficiently with a very good text editor (Kate in linux, but there is a windows version too) which has some tools to work with
python and with projects too. But it is not an IDE.

A very nice way to work, but again the tool I'm using is linux-specific is to have a program that watches changes in my files while I'm editing, and automatically runs the code every time I save it in the editor. It gives an immediate feedback about what I'm doing. The program runs in a terminal next to the editor window and it replaces an IDE. I know there are windows tools to do such things but I don't know them.

Edited by Gribouillis

Votes + Comments
Good approach. I shall follow it.

Sounds good. Good advice, as always. Thanks. I'll stick with the regular old tutorials. So far, I'm liking Python a lot. Wish I'd learned it sooner. Solved.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.