A detailed explanation of the differences between import and reload and __import__ in Python

Source: Internet
Author: User

This article mainly introduces the difference between the import reload __import__ in Python, the needs of friends can refer to the following

Import

Role: Import/introduce a Python standard module that includes a. py file, a directory with a __init__.py file (custom module).


Import Module_name[,module1,...] from module import *|child[,child1,...]

Note: When the import statement is reused multiple times, the specified module is not reloaded, only the memory address of the module is referenced to the local variable environment.

Instance:

pythontab.py


#!/usr/bin/env python  #encoding: utf-8 import os print ' in Pythontab ', ID (OS)

test.py


#!/usr/bin/env python  #encoding: utf-8 import pythontab  #第一次会打印pythontab里面的语句 import os #再次导入os后, Its memory address is the same as the pythontab inside, so here is just a local reference to the OS print ' in C ', ID (OS) import Pythontab #第二次不会打印pythontab里面的语句, because there is no reload

Reload

Function: The module has been loaded to reload, generally used for the original module has changed and other special cases, reload before the module must have been import.


Import osreload (OS)

Description

The reload will reload the loaded module, but the original instance will still use the old module, and the newly produced instance uses the new module, or the original memory address after reload; Import: The module in the format is reloaded.

Instance:

pythontab.py


#!/usr/bin/env python  #encoding: utf-8 import os print ' in Pythontab ', ID (OS)

test.py


#!/usr/bin/env python  #encoding: utf-8 import pythontab  #第一次import会打印pythontab里面的语句 print ID (pythontab) # The original Pythontab memory address reload (Pythontab) #第二次reload还会打印pythontab里面的语句 because of the reload print ID (pythontab) #reload后pythontab的内存地址, Just like the original.

Extended:

It says that the reload function will be used under special circumstances, and in addition to the original module files, there are cases where the reload function needs to be used, for example.


#!/usr/bin/env python  #encoding: utf-8 import sys  #引用sys模块进来 is not the first time the SYS is loaded reload (SYS) #重新加载sys Sys.setdefaultencoding (' UTF8 ') # #调用setdefaultencoding函数

The code above is correct, then test the following code


#!/usr/bin/env python  #encoding: utf-8 import sys   sys.setdefaultencoding (' UTF8 ')

The above test will fail, so why do you have to reload the SYS module first when calling Setdefaultencoding? Because the import statement here is actually not SYS's first import statement, that is, this may be the second to third time the Sys module imports, here is only a reference to the SYS, only reload can be reloaded; then why Reload? Instead of calling the function directly? Because the setdefaultencoding function is deleted after being called by the system, it is not already in the import reference, so it must be reload once sys module, so setdefaultencoding will be available To modify the current character encoding of the interpreter in the code. Try the following code, which will also give an error:


#!/usr/bin/env python  #encoding: utf-8 import sys  reload (SYS)  sys.setdefaultencoding (' UTF8 ')  del Sys.setdefaultencoding  # #删除原来的setdefaultencoding函数   sys.setdefaultencoding (' gb2312 ')

So who actually imported the SYS and called the setdefaultencoding function? The answer is in the Python installation directory of the Lib folder, there is a file called site.py "python2.6", in which you can find main ()--setencoding ()-->sys.setdefaultencoding ( encoding), because the site.py is automatically loaded each time the Python interpreter is started, the main function is executed every time, and the setdefaultencoding function is removed.

__import__

Role:

The same function as the import statement, but __import__ is a function, and only receives the string as a parameter, so it can be imagined. In fact the import statement is called this function for importing work, import sys <==>sys = __import__ (' sys ')

Use:


__import__ (module_name[, globals[, locals[, FromList]]) #可选参数默认为globals (), locals (), []__import__ (' OS ')  __ import__ (' OS ', Globals (), locals (), [' Path ', ' Pip ']) #等价于from OS import path, Pip

Description

This function is usually used when loading dynamically, such as when you want to load the module under a folder, but the module name under it changes frequently, you can use this function to load all modules dynamically, the most common scenario is the plug-in function support.

Extended:

Now that the module can be imported dynamically through strings, can I dynamically reload the module through a string? Try reload (' OS ') direct error, is there no other way? Although you cannot reload directly, you can unimport a module first, and then __import__ to reload the module. Now look at how the Unimport operation is implemented, in the Python interpretation can be globals (), locals (), VARs (), dir () and other functions to see the current environment loaded modules and their location, but these can only be seen can not be deleted, so cannot unimport But there is another place that is dedicated to the module, this is Sys.modules, through Sys.modules can see all the loaded and successful modules, and more than Globals, the default will load some additional modules, followed by Unimport.


#!/usr/bin/env python  #encoding: utf-8 import sys __import__ (' a ')   #第一次导入会打印消息 del sys.modules[' a ']  # Unimport __import__ (' a ')  #再次导入还是会打印消息 because it has been unimport once __import__ (' a ')  #这次就不会打印消息了

Summarize

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.