In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to migrate applications to Python3". In daily operation, I believe many people have doubts about how to migrate applications to Python3. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how to migrate applications to Python3". Next, please follow the editor to study!
1. Use 2to3
Since a few years ago, Python has come with a script called 2to3 that helps you automatically convert most of your code from Python 2 to Python 3 without your knowledge.
Here is a piece of code written in Python 2.6:
#! / usr/bin/env python #-*-coding: utf-8-*-mystring = u'abcd é 'print ord (mystring [- 1])
Execute a 2to3 script on it:
$2to3 example.py RefactoringTool: Refactored example.py-example.py (original) + example.py (refactored) @ @-1 2to3 example.py RefactoringTool 5 + 1Magne5 @ @ #! / usr/bin/env python #-*-coding: utf-8-*-mystring = u'abcd é'- print ord (mystring [- 1]) + mystring = 'ABCD é' + print (ord (mystring [- 1]) RefactoringTool: Files that need to be modified: RefactoringTool: example.py
By default, 2to3 will only mark the code that must be modified when migrating to Python 3, and the Python 3 code shown in the output is directly available, but you can add-w or-- write parameter to 2to3 so that it can modify your Python 2 code file directly according to the scheme given.
$2to3-w example.py [...] RefactoringTool: Files that were modified: RefactoringTool: example.py
The 2to3 script is not only valid for a single file, you can also use it for all Python files in a directory, and it also works recursively for Python files in all subdirectories.
2. Use Pylint or Pyflakes
It is not uncommon for some bad code to run under Python 2 without exception, while running under Python 3 will report more or less errors. Because these bad code cannot be fixed by syntax conversion, 2to3 has no effect on them, but it produces an error once it is run with Python 3.
To find out, you need to use tools such as Pylint, Pyflakes (or flake8 wrapper). I prefer Pyflakes, which ignores differences in code style, which is different from Pylint. Although code elegance is a feature of Python, at the level of code migration, "keeping the code functional consistent" is undoubtedly much more important than "keeping the code style consistent".
The following is a sample output from Pyflakes:
$pyflakes example/maths example/maths/enum.py:19: undefined name 'cmp' example/maths/enum.py:105: local variable 'e'is assigned to but never used example/maths/enum.py:109: undefined name' basestring' example/maths/enum.py:208: undefined name 'EnumValueCompareError' example/maths/enum.py:208: local variable 'e'is assigned to but never used
The above output by Pyflakes clearly shows the problems that need to be modified in the code. By contrast, Pylint outputs up to 143 lines of content, most of which are trivial issues such as code indentation.
It is worth noting that line 19 is a misleading error. From the output, you might think that cmp is an undefined variable before use, but in fact cmp is a built-in function in Python 2, which has been removed in Python 3. And this code is placed in the try statement block, which is easy to ignore unless the output value of this code is carefully examined.
Try: result = cmp (self.index, other.index) except: result = 42 return result
During the code migration, you will find that many functions that used to work in Python 2 have changed and even been removed directly in Python 3. For example, the binding mode of PySide has changed, importlib has replaced imp, and so on. Such a problem can only be solved one by one, and you need to weigh whether the functions involved need to be refactored or abandoned directly. But for now, most of the problems are known and well documented. So the difficulty is not to fix the problem, but to find the problem, and from this point of view, using Pyflake is necessary.
3. Fix the damaged Python 2 code
Although 2to3 scripts can help you modify your code to be compatible with Python 3, it is a bit powerless for a complete code base, as some old code may need a different structure in Python 3. In this case, it can only be modified manually.
For example, the following code works properly in Python 2.6:
Class CLOCK_SPEED: TICKS_PER_SECOND = 16 TICK_RATES = [int (I * TICKS_PER_SECOND) for i in (0.5,1,2,3,4,6,8,11,20)] class FPS: STATS_UPDATE_FREQUENCY = CLOCK_SPEED.TICKS_PER_SECOND
Automated tools like 2to3 and Pyflakes won't find the problem, but if the above code is run using Python 3, the interpreter will assume that CLOCK_SPEED.TICKS_PER_SECOND is not explicitly defined. So you need to change the code to an object-oriented structure:
Class CLOCK_SPEED: def TICKS_PER_SECOND (): TICKS_PER_SECOND = 16 TICK_RATES = [int (I * TICKS_PER_SECOND) for i in (0.5,1,2,3,4,6,8,11,20)] return TICKS_PER_SECOND class FPS: STATS_UPDATE_FREQUENCY = CLOCK_SPEED.TICKS_PER_SECOND ()
You might think that rewriting TICKS_PER_SECOND () to a constructor (setting the default value with the _ _ init__ function) will make the code look more concise, but you need to change the calling form of this method from CLOCK_SPEED.TICKS_PER_SECOND () to CLOCK_SPEED (), which will more or less have some unknown impact on the entire library. If you are familiar with the structure of the entire code base, you can indeed make such changes as you like. But I usually think that as long as I make changes, it may affect at least three places in other code, so I prefer not to change the structure of the code.
At this point, the study on "how to migrate applications to Python3" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 239
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.