upgrade python scripts from python 2 to python 3 - python-3.x

I have many scripts I need to upgrade from Python 2 to Python 3. I am familiar with some of the common changes to make, such as print a vs print(a) and iter[items|keys|etc]. I use 2to3 which seems to cover several of the changes needed, but misses others (runtime issues, bytes, unicode, etc). Is there a more complete script than 2to3, or something else that would cover most of the necessary changes?

Related

Things to update manually after python 2 to python 3 migration using an automated tool

I have been trying to migrate an older project which is mainly python 2.7 compatible to python 3.9. I did go through couple of python migration documentation and I have decided to use 2to3 tool for the migration. I read a couple of blogs that mention that these automated tools do not handle everything and there are a couple of things that have to be changed manually (for eg. - the use of division operator, text vs binary data, etc.).
Can someone tell me what else should be manually updated and what are some things that I should look out for ?

What is the real current status of Twisted on Python3?

Had used some twisted using Python 2.6 few years back, and since then stopped using Python. Recently starting picking up on Python 3, and was checking status of twisted support for Python 3 which was very thin back when I left.
Introduction section of the Latest document on the topic says this:
Twisted is currently being ported to work with Python 3.4+. This
document covers Twisted-specific issues in porting your code to Python
3.
Most, but not all, of Twisted has been ported, and therefore only a
subset of modules are installed under Python 3. You can see the
remaining modules that need to be ported at
twisted.python._setup.notPortedModules, if it is not listed there,
then most of all of that module will be ported.
And clicking on the twisted.python._setup.notPortedModules shows no module listed.
Does that mean that twisted is now fully supported on Python 3 ? Or just the list is incorrect ? If so, are the samples / examples converted for Python 3 ?
The most likely definitive resource on this topic is the continuous integration system.
According to https://travis-ci.org/twisted/twisted, a recent run had 9957 passing tests on Python 3.6 compared to 9933 passing tests on Python 2.7. There is some slop in these numbers because the test suite includes some tests which are only relevant to Python 2.x and others which are only relevant to Python 3.x (therefore we would not expect exactly the same number of tests to run on each runtime) however these numbers are so close that I would say that Twisted has basically been complete ported to Python 3.6. Problems that remain are probably more likely to be "regular bugs" rather than unported code.

How to see every dependency in a Python program (e.g. imports)?

I have several apps I'm developing that are for end users that have no idea how to use Python. I have already discovered how to setup a package that allows them to run any script without Python knowledge but I don't know how to minimize the distribution size by only including subsets (I.e. the actual function calls in large libs like NumPy) of each imported library that are required. Is there a way to output the actual subcomponents of each imported library that are actually accessed during the function? All my internet searches end up with cyclical imports which is not what I need. There must be some Python dependency walker equivalent I have yet to discover. Much appreciated any libs that can outline this.
[UPDATE]
I converted Snakefood 1.4 over to Python 3x (3.5 tested to build) with python setup.py install and saved it here: https://github.com/mrslezak/snakefood per the accepted answer.
Use Snakefood
Here's a command
sfood -i -r myscript.py | sfood-cluster > dependencies.txt

Why doesn't pyGame or pyglet support python 3?

I have been looking into various game design modules for python such as pyglet and pyGame. I have noticed that both of these seem to only be compatible with python 2. Seeing as python 3 is the most current version, why is this? My experience with python has been with python 3, so are there good frameworks/modules out there for python 3 game development? Or would I be better off learning python 2 and using pyglet or pyGame?
edit: I would be using pyGame on a mac. The downloads page, http://www.pygame.org/download.shtml, only has links for py2.6, py2.5, and py2.4, which is what leads me to believe it is not python 3 compatible.
I don't even get your question, as PyGame is compatible with Python 3 since version 1.9:
http://www.pygame.org/wiki/python3porting?parent=todo
There are a few minor issues, but it is usable with Python 3.
I'm developing a pygame-based application on Mac OSX with python 3. It definitely works fine, and so far I haven't run into any compatibility problems, though there are certainly other mac-specific pygame bugs that I have found.
That said, I can definitely confirm that it works.
The fifth link from the top under "Windows" on the page you linked to clearly has py-3.1 in the name.
They don't support Python 3, because nobody has made them support Python 3 yet. It's a bizarre question. :-)
PyGame seems to mostly support Python 3. There are some issues: (from http://www.pygame.org/wiki/python3porting?parent=todo )
Complete unicode file name handling (fsencoding branch), then merge back into trunk (by mid Sept/10).
update documentation to explain Unicode in Pygame
work out final solution for open Python 3 IOBase objects: how to check abstract types from C. (Sept. 1, 2010) Probably the most practical solution is to just do duck typing.
_movie - deferred until module is ready for release
scrap (does some C string stuff that makes porting difficult)
camera - what is the status of this module?
Nowhere does it say that it doesn't work for Python 3 on mac, and it seems unlikely to that that this would be the case. So you can try, but you will have to compile it yourself.
Pyglet doens't mention Python 3, so it's probably not ported.
pyglet 1.2 (alpha as of July 2012) works with Python 3.

How to Be Python 3 Ready?

What are the current rules for writing python code that will pass cleanly through 2to3 and what are the practices that seem to be best suited to writing code that will not become mired forever in version 2.
I have read from the SciPy/NumPy forums that "100% test coverage" (unit testing) is important for many people, and I am not sure if that would apply to everybody. Certainly having a reasonable set of unit tests to try your code out with after conversion, seems a sane step.
Are there other things? What are skilled Pythonistas doing if they are writing 2.x code that they hope to have come through "cleanly" in the 2to3 process.
I am looking for specific instances of "[don't] do this" as well as some more general "best-practices", but specific instances of "do's and don'ts" are helpful.
Let's assume that frameworks, libraries (Django, SciPy/NumPy), and every other C Extension we need gets ported to Python3 eventually, and I'm asking about how you write and maintain the pure python language code that you write yourself.
Update: It's possible that what I really want is the "style guide" and list of deprecated features that everybody was already staying away from. I cut my teeth on Python 1.5 and moved to 2.0, and then have not really followed much of the 2.5/2.6 era, used them but really my code is more 2.1 era.
I'd say:
Read the "What's new for Python 3.0". Very informative.
In particular, if you care about Unicode or text encodings at all, take the time to understand what has changed for 3.x. That's probably one of the trickier things to change for Python 3.x.
Get Python 2.6 or 2.7, and run your code with the -3 flag. It will tell you about things in your code that will need changing.
Before using 3rd-party packages, check to see if they have a Python 3.x version. If not, check the package web site, mailing lists, version control repositories etc to see how actively the package is being developed and whether there is a roadmap towards Python 3.x support.
Download Python 3.x and try it out! Admittedly, that might not be practical if you care about code that currently depends on packages that don't yet support Python 3.x (e.g. wxPython or Django).

Resources