In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to make the script compatible with Python2 and Python3". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to make the script compatible with Python2 and Python3 at the same time.
The problem that developers often encounter is that there is a famous Python module that is used by many developers, but not all of them only use Python2 or Python3. At this point, you have two options. The first way is to develop two modules for Python2 and Python3. Another way is to adjust your current code to be compatible with both Python2 and Python3. This article will show you some ways to make your script compatible with both Python2 and Python3.
Future module guide
This is also the most important method, which is to guide the _ _ future__ module. It can help you guide the functions of Python3 in Python2. Here is an example of a group. The up and down manager is a new feature of the Python2.6+ quote, which can be done if you want to make it available in Python2.5:
From _ _ future__ import with_statement
Print has become a function in Python3. If you want to make it available via _ _ future__ guide in Python2:
# Output:
From _ _ future__ import print_function
Print (print)
# Output:
Module renaming
First, many times we will guide the module in the script like this:
Import foo
# or
From foo import bar
Actually, you can also do this:
Import foo as foo
This does the same thing as the uploaded code, but most importantly it makes your script compatible with both Python2 and Python3. Now let's take a look at the code for coach:
Try:
Import urllib.request as urllib_request # for Python 3
Except ImportError:
Import urllib2 as urllib_request # for Python 2
Explain the "down and up" code a little bit. We wrap the module guide code in a try/except statement. We do this because there is no urllib.request module in Python 2. This will cause an ImportError exception. The function of urllib.request in Python2 is provided by urllib2. So, when we try to guide the urllib.request module in Python2, we capture the ImportError and we will replace it by guiding the urllib2 module.
Finally, you need to know how to use the as keyword. It maps the module of the guide to urllib.request, so we can use the alias urllib_request to make all the classes and methods in the import urllib2.
Out of date Python2 built-in functionality
The other thing to know is that 12 built-in features in Python2 have been removed from Python3. Make sure that these features do not appear in your Python2 code to ensure compatibility with Python3. Here are two ways to force you to give up 12 built-in features:
From future.builtins.disabled import *
Now, whenever you try to destroy these abandoned modules in Python3, a NameError exception will be thrown, as follows:
From future.builtins.disabled import *
Apply ()
# Output: NameError: obsolete Python 2 builtin apply is disabled
Backward compatible external support of the standard library
Some packages provide Python2 with the function of Python3 under the control of the official. For example, we have:
Enum pip install enum34
Singledispatch pip install singledispatch
Pathlib pip install pathlib
Thank you for reading, the above is the content of "how to make the script compatible with Python2 and Python3". After the study of this article, I believe you have a deeper understanding of how to make the script compatible with Python2 and Python3 at the same time. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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: 240
*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.