Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to migrate the project to Python3 perfectly

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to migrate the project to Python3 perfectly". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to migrate the project to Python3 perfectly.

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:

Execute a 2to3 script on it:

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.

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:

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.

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:

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:

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, I believe you have a deeper understanding of "how to migrate the project to Python3 perfectly". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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: 0

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report