Import,reload in Python, and __import__

Source: Internet
Author: User

Import
Role:
Import/introduce a Python standard module that includes a. py file, a directory with a __init__.py file.
e.g:

[Python]View Plaincopy
    1. Import Module_name[,module1,...]
    2. From module import *|child[,child1,...]

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

[Python]View Plaincopy
  1. a.py
  2. #!/usr/bin/env python
  3. #encoding: Utf-8
  4. Import OS
  5. Print ' In a ', ID (OS)
  6. m.py
  7. #!/usr/bin/env python
  8. #encoding: Utf-8
  9. Import a #第一次会打印a里面的语句
  10. The import OS #再次导入os后, whose memory address is the same as the one inside, so this is just a local reference to the OS
  11. Print ' in M ', ID (OS)
  12. Import a #第二次不会打印a里面的语句 because there is no reload


Reload
Role:
The modules that have already been loaded are reloaded, usually for special cases where the original module has changed, and the module must have been reload before.
e.g:
Import OS
Reload (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.
Test:

[Python]View Plaincopy
  1. a.py
  2. #!/usr/bin/env python
  3. #encoding: Utf-8
  4. Import OS
  5. Print ' In a ', ID (OS)
  6. m.py
  7. #!/usr/bin/env python
  8. #encoding: Utf-8
  9. Import a #第一次import会打印a里面的语句
  10. Print ID (a) #原来a的内存地址
  11. Reload (a) #第二次reload还会打印a里面的语句 because there is a reload
  12. Print ID (a) #reload后a的内存地址, same as 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.

[Python]View Plaincopy
    1. #!/usr/bin/env python
    2. #encoding: Utf-8
    3. Import sys #引用sys模块进来, not the first time the SYS was loaded
    4. Reload (SYS) #重新加载sys
    5. sys.setdefaultencoding (' UTF8 ') # #调用setdefaultencoding函数

The code above is correct, then test the following code

[Python]View Plaincopy
    1. #!/usr/bin/env python
    2. #encoding: Utf-8
    3. Import Sys
    4. 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:

[Python]View Plaincopy
    1. #!/usr/bin/env python
    2. #encoding: Utf-8
    3. Import Sys
    4. Reload (SYS)
    5. sys.setdefaultencoding (' UTF8 ')
    6. Del sys.setdefaultencoding # #删除原来的setdefaultencoding函数
    7. 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 the site.py file has such a piece of code:

[Python]View Plaincopy
    1. If Hasattr (sys, "setdefaultencoding"):
    2. Del sys.setdefaultencoding

So when the syssetdefaultencoding function is out, it has been deleted.

__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 ')
e.g:

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

[Python]View Plaincopy
      1. #!/usr/bin/env python
      2. #encoding: Utf-8
      3. Import Sys
      4. __import__ (' a ') #第一次导入会打印消息
      5. Del sys.modules[' a '] #unimport
      6. __import__ (' a ') #再次导入还是会打印消息 because it's been unimport once.
      7. __import__ (' a ') #这次就不会打印消息了

Import,reload in Python, and __import__

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.