One: module
A module is a py file, which defines some business functions. Reference modules, which can be imported using Import statements. After the module is imported, the function in the module is used by the module. Function name (parameter) . If there are multiple modules with the same name, the previous module name needs to be added to the package path.
If we only need to import several functions in the module, we can use the From Module name Import function name, function name,.. , so that you can use the function name in your code to invoke the function without a path.
To avoid conflicts with the same name, you can also set an alias at the same time as the import function, from themodule name, import function name as Alias , and then in code you can use the alias to invoke the function.
Two: Package
In order to avoid conflicts of the same name, the module is usually managed in the package and imported by the full path.
A package in Python is a special kind of folder that has a _init_.py file at each level, even if the contents of the file are empty. The purpose of this file is to identify the folder as a package.
To facilitate the invocation of the methods in the module below, aliases can also be used when importing the module: Import module as alias
Three: Dynamic Import module
Python Import module error will be reported Importerror exception, can be based on this feature, catch import exception to dynamically import. Such as:
Try : from Import Stringio except importerror: from Import Stringio
IV: Use the new version of the module in the old version
When a feature of a new version is incompatible with an older version, the feature will be added to the __future__ module in the old version so that the old code can test the new feature in the old version. So you can import the __future__ module in the old version of the Python program, and then call the new version of the function.
V: Use of third-party modules
Python official provides the pip install third-party module name. PY directive, you can find the name of the third-party module to use on the Https://pypi.python.org/pypi Central warehouse, and then use the command line to install it.
(similar to the practice of Maven import dependency packages in Java development)
Python Learning Note III: Modules