import
Function: Import/introduce a python standard module, which includes .py files, directories with __init__.py files (custom modules).
import module_name[,module1,...] from module import *|child[,child1,...]
Note: Repeated use of the import statement does not reload the specified module, but only references the memory address of the module to the local variable environment.
Example:
#!/usr/bin/env python #encoding: utf-8 import os print 'in pythontab',id(os)
#!/usr/bin/env python #encoding: utf-8 import pythontab # The first time it prints the statement inside pythontab import os # After importing os again, the memory address is the same as in pythontab, so this is just a local reference to os. print 'in c',id(os) import pythontab #It won't print the second time.pythontabInside the statement,Because there's no reloading
reload
Role: reload the loaded module, generally used for the original module has changed and other special circumstances, reload before the module must have been imported.
import os reload(os)
Description:
reload will reload the loaded module, but the original instance that has been used will still use the old module, while the newly produced instance will use the new module; the original memory address will still be used after reload; it cannot support the format of from. import. format for reloading modules.
Example:
#!/usr/bin/env python #encoding: utf-8 import os print 'in pythontab',id(os)
#!/usr/bin/env python #encoding: utf-8 import pythontab #The first import prints the statements inside pythontab. print id(pythontab) # Memory address of the original pythontab reload(pythontab) # The second reload will also print the statements inside pythontab, since there is a reload print id(pythontab) #reloadempresspythontabmemory address,Same as before.
Expansion:
Said above, in the special circumstances of the use of the reload function; in addition to the original module file has been modified, what other cases need to use the reload function, here is an example.
#!/usr/bin/env python #encoding: utf-8 import sys #Referencing the sys module in doesn't do a first load of sys reload(sys) #Reload sys ('utf8') ##call (programming)setdefaultencodingfunction (math.)
The above code is correct, then test the following code
#!/usr/bin/env python #encoding: utf-8 import sys ('utf8')
The above test will fail, so why do you have to reload the sys module when calling setdefaultencoding? Because the import statement here is not actually the first import statement of sys, that is to say, here may actually be the second or third time to import the sys module, here is just a reference to sys, only reload can be reloaded; then why reload, and directly referenced over the function can not be called? Because the setdefaultencoding function has been deleted after being called by the system, so when it is referenced through the import, it is actually no longer available, so the sys module must be reloaded once, so that setdefaultencoding will be available to modify the current character encoding of the interpreter in the code. Try the following code, the same error will be reported:
#!/usr/bin/env python #encoding: utf-8 import sys reload(sys) ('utf8') del ## Remove the original setdefaultencoding function ('gb2312')
So who actually imported sys and called the setdefaultencoding function before? The answer lies in the Lib folder of the python installation directory, in a file called [python2.6], where you can find main() --> setencoding()-->(encoding), because this is automatically loaded every time you start the python interpreter, so the main function is executed every time. The setdefaultencoding function has been removed as soon as it comes out.
__import__
Role:
Same function as the import statement, but __import__ is a function and only takes strings as arguments, so its role can be imagined. The import statement actually calls this function to do the importing. import sys <==>sys = __import__('sys')
Use:
__import__(module_name[, globals[, locals[, fromlist]]]) #Optional arguments default to globals(), locals(), []. __import__('os') __import__('os',globals(),locals(),['path','pip']) #equivalencefrom os import path, pip
Description:
Usually in the dynamic loading can be used to this function, for example, you want to load a folder used under the module, but the name of the module under the module will often change, you can use this function to dynamically load all the modules, the most common scenario is the plug-in feature support.
Expansion:
Since it is possible to dynamically import a module via a string, is it possible to dynamically reload the module via a string? Try reload('os') which reports an error directly, is there no other way? Although you can't reload directly, you can unimport a module first, and then __import__ to reload the module. Now look at the unimport operation how to realize, in Python interpretation can be globals (), locals (), vars (), dir () and other functions to view the current environment and the location of the loaded module, but these can only be seen can not be deleted, so it can not be unimported; however, in addition to there is a place to specialize in storing module This is, through you can view all the loaded and successful modules, and more than globals, that the default will load some additional modules, the next is unimport.
#!/usr/bin/env python #encoding: utf-8 import sys __import__('a') # The first import prints a message del ['a'] #unimport __import__('a') # Importing again still prints the message because it's already unimported once __import__('a') #It won't print the message this time.
summarize
The above is a small introduction to the python import reload __import__ difference in detail, I hope to help you, if you have any questions please leave me a message, I will promptly reply to you. I would also like to thank you very much for your support of my website!