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

What is the function of absolute_import?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "what is the role of absolute_import". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the role of absolute_import".

From the literal meaning, it is clear that the function of this function is the absolute path import, on the contrary, the corresponding is the relative path.

The difference between absolute and relative imports works only when importing modules from a package and other sub-modules from a package. Take the following e.g. For example, there is a string.py file under pkg, which has the same name as the string module in the python standard library, in which the main.py file has a reference to the string module. When we import main.py from the pkg package, if there is no absolute_import, we will reference the string module under pkg, otherwise, we will find the string standard library from the top-level path.

Liang@iZ250k0mjlbZ:~$ mkdir pkgliang@iZ250k0mjlbZ:~$ touch pkg/__init__.pyliang@iZ250k0mjlbZ:~$ touch pkg/string.pyliang@iZ250k0mjlbZ:~$ echo 'import string Print (string.ascii_uppercase)'> pkg/main1.pyliang@iZ250k0mjlbZ:~$ pythonPython 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609] on linux2Type "help", "copyright", "credits" or "license" for more information. > > import pkg.main1.pyTraceback (most recent call last): File ", line 1, in File" pkg/main1.py ", line 1, in import string Print (string.ascii_uppercase) AttributeError: 'module' object has no attribute' ascii_uppercase' > import pkg.main1Traceback (most recent call last): File ", line 1, in File" pkg/main1.py ", line 1, in import string;print (string.ascii_uppercase) AttributeError: 'module' object has no attribute' ascii_uppercase' > > import pkg.string > exit () liang@iZ250k0mjlbZ:~$ echo 'from _ _ future__ import absolute_import;import string Print (string.ascii_uppercase)'> pkg/main2.pyliang@iZ250k0mjlbZ:~$ pythonPython 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609] on linux2Type "help", "copyright", "credits" or "license" for more information. > > import pkg.main2ABCDEFGHIJKLMNOPQRSTUVWXYZ > exit () unicode_literals effect

Literally, you can simply see that it is aimed at unicode coding, which involves the coding history of python. Python2 defaults to ascii encoding, and for unicode-encoded strings, you must add uforce e.g. U "test" to the signature. This headache has been solved in python3. The default coding uses unicode coding. The function of this library is for the transition from python2 to python3, once introduced. The default encoding under the current file is unicode, and the following example directly shows its purpose:

Python 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609] on linux2Type "help", "copyright", "credits" or "license" for more information. > > from _ future__ import unicode_literals > Avocu "aaa" > > au'aaa' > a = "aaa" > > au'aaa' > Thank you for your reading. This is the content of "what is the function of absolute_import"? I believe that you have a deeper understanding of what the role of absolute_import is, and the specific use needs to be verified in practice. 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: 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

Internet Technology

Wechat

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

12
Report