In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to achieve module import in Python", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve module import in Python" this article.
This article does not discuss the import mechanism of Python (the underlying implementation details), but only the concepts related to modules and packages, as well as import statements. Typically, the import module uses the following statement:
Import... import... As... from... Import... from... Import... As...
In general, it is sufficient to use the above statement to import the module. However, in some special scenarios, other import methods may be required. For example, Python also provides _ _ import__ built-in function and importlib module to implement dynamic import. The advantage of dynamic import is that the loading of modules can be delayed and import actions are supported only when modules are used.
It is true that the delayed loading of the module can be achieved by using the _ _ import__ function and the importlib module, but its disadvantage is that the same import statement should be implemented wherever the specified module is needed, which is not easy to maintain and very troublesome. It is a better choice if you can implement lazy import at the top level, which is the point that will be discussed at the end of this article.
Before discussing some advanced usage, you need to understand the concept of modules and packages.
Modules and packages
A module can be understood as a code file that Python can load and execute, and the code file can be not only .py files, but also other types of files such as .so. Python has only one module object type, and all modules are of this type. To facilitate the organization of multiple modules and to provide the naming of a module hierarchy, Python provides the concept of packages.
You can simply think of a package as a directory of a file system, and a module as a code file in a directory. (note that this is not entirely true, because packages and modules come not only from the file system, but also from compressed files, networks, etc.). Similar to the directory structure of a file system, packages are organized hierarchically, and the package itself can contain subpackages and regular modules.
The package can actually be seen as a special module. For example, the directory of the regular package (the concept of the regular package is described below) needs to contain the _ _ init__.py file. When the package is imported, the top-level code of the file is implicitly executed, just as the top-level code is executed when the module is imported, and the file is like the code of the package. So the package is a special module. It is important to keep in mind that all packages are modules, but not all modules are packages. Both subpackages and modules in a package have a _ _ path__ attribute, specifically, any module that contains a _ _ path__ attribute is considered a package. All modules have a name, similar to the standard property access syntax, with child packages separated from the names of their parent packages.
Python defines two types of packages, the regular package and the namespace package. Regular packages are traditional packages that exist in Python 3.2 and earlier. The regular package is the directory that contains the _ _ init__.py file. When importing a regular package, the _ _ init__.py file is implicitly executed, and the objects it defines are bound to the name in the package namespace. The _ _ init__.py file can contain the same Python code that any other module can contain, and Python will add some additional attributes to the module on import.
Starting with Python 3. 3, Python introduced the concept of namespace packages. A namespace package is a composite of different filesets, each fileset contributing a child package to the parent package, and all packages do not need to include the _ _ init__.py file. Filesets can be stored in different locations on the file system. The lookup of the fileset includes compressed files, networks, or other places that Python searches during the import process. Namespace packages can or may not correspond directly to objects in the file system, and they can be real modules without a specific description. For an updated description of the namespace package, refer to PEP 420.
The _ _ path__ attribute of the namespace package differs from the regular package in that it traverses all paths that contain the command space package using a custom iterator type. If the path of their parent package (or the sys.path of the higher-level package) changes, it will automatically re-search the package portion of the package the next time it tries to import.
If you have the following directory structure:
.
├── bar-package
│ └── nsp
│ └── bar.py
└── foo-package
└── nsp
└── foo.py
Then nsp can be a namespace package, and the following is the test code (remember to run the test with Python 3.3 and later):
Import syssys.path.extend (['foo-package',' bar-package']) import nspimport nsp.barimport nsp.fooprint (nsp.__path__) # output: # _ NamespacePath (['foo-package/nsp',' bar-package/nsp'])
The namespace package has the following features:
1. The lowest priority, after all the existing versions of import rules
2. You no longer need to include the _ _ init__.py file in the package
3. Code with scattered directories can be imported and organized.
4. Rely on the search order from left to right in sys.path
_ _ import__
The _ _ import__ function can be used to import the module, and the import statement also calls the function. It is defined as:
_ _ import__ (name [, globals [, locals [, fromlist [, level])
Parameter description:
Name (required): name of the loaded module
Globals (optional): a dictionary containing global variables, which is rarely used, with the default value global ()
Locals (optional): a dictionary containing local variables, which are not used by the internal standard implementation, using the default value-local ()
Fromlist (Optional): name of the imported submodule
Level (Optional): import path option, which defaults to-1 in Python 2, which means that both absolute import and relative import are supported. The default in Python 3 is 0, which means that only absolute import is supported. If it is greater than 0, it represents the series relative to the imported parent directory, that is, 1 is similar to'. 'and 2 is similar to'..'.
Examples of use:
# import spamspam = _ _ import__ ('spam') # import spam.hamspam = _ _ import__ (' spam.ham') # from spam.ham import eggs, sausage as saus_temp = _ _ import__ ('spam.ham', fromlist= [' eggs', 'sausage']) eggs = _ temp.eggssaus = _ temp.sausage module cache
When performing a module import, Python's import system first attempts to find it from sys.modules. Sys.modules is a cache of all imported modules, including intermediate paths. That is, if foo.bar.baz is imported, sys.modules will contain the cache into the foo,foo.bar and foo.bar.baz modules. In fact, a dict type, each key has its own value, corresponding to the corresponding module object.
During the import process, first look for the module name in sys.modules, and if it exists, return to the module and end the import process. If the module name is not found, Python continues to search for the module (find and load from sys.path). Sys.modules is writable, deleting a key causes the cache implementation of the specified module, and the next import will re-search the specified module, similar to the module's reload.
It is important to note that if you keep the module object reference, invalidate the cache in sys.modules, and then re-import the specified module, the two module objects are different. By contrast, when importlib.reload () reloads the module, it uses the same module object and simply reinitializes the module content by rerunning the module code.
Imp and importlib module
The imp module provides some interfaces that are implemented within import statements. For example, module lookup (find_module), module loading (load_module), and so on (the module import process includes module lookup, loading, caching, and so on). You can use this module to simply implement the built-in _ _ import__ function:
Import impimport sysdef _ _ import__ (name, globals=None, locals=None, fromlist=None): # first find try from the cache: return sys.modules [name] except KeyError: pass # if it is not in the module cache Then start to find the module fp, pathname, description = imp.find_module (name) from sys.path # how to find the module and load it into try: return imp.load_module (name, fp, pathname, description) finally: if fp: fp.close ()
The importlib module is created in python 2.7and contains only one function:
Importlib.import_module (name, package=None)
This function encapsulates _ _ import__ for a more convenient dynamic import module. For example, use it to achieve relative import:
Import importlib# is similar to 'from. Import breadb = importlib.import_module (' .b', _ _ package__)
Starting with python 3, the built-in reload function has been moved to the imp module. Since Python 3.4, the imp module has been rejected and is no longer recommended, and the functions it contains have been moved to the importlib module. That is, starting with Python 3.4, the importlib module is a collection of previous imp modules and importlib modules.
Lazy import
Most of the previous content is to pave the way for the implementation of lazy import, the rest of the content is just an extension (just a little bit more). Lazy import is to delay module import, and the import action of the module is performed only when the module is actually used, and never happens if the module is not used.
The need for lazy import is still common. It is generally recommended that modules are imported only at the top level, and sometimes importing modules at the top level is not the best choice. For example, when a module is used only in a function or class method, you can use a local import (performing the import in the local scope) so that the module is imported only when the function or method is executed. this avoids introducing module variables into the top-level namespace. For example, in the project I am responsible for, I need to use the pandas package, and the import of the pandas package will take up some memory (not much, but not a little, tens of megabytes), so when the pandas package is not used, we hope it will not be imported. Some of the packages we implement ourselves can be time-consuming to load (because it takes a few seconds to more than a dozen seconds to import to read the configuration, etc.), so lazy import is extremely needed.
The following is a simple implementation of lazy import for reference:
Import sysfrom types import ModuleTypeclass LazyModuleType (ModuleType): @ property def _ mod (self): name = super (LazyModuleType, self). _ _ getattribute__ ("_ _ name__") if name not in sys.modules: _ import__ (name) return sys.modules [name] def _ getattribute__ (self, name): if name = "_ mod": return super (LazyModuleType) Self). _ getattribute__ (name) try: return self._mod.__getattribute__ (name) except AttributeError: return super (LazyModuleType, self). _ getattribute__ (name) def _ setattr__ (self, name, value): self._mod.__setattr__ (name, value) def lazy_import (name) Package=None): if name.startswith ('.'): if not package: raise TypeError ("relative imports require the 'package' argument") level = 0 for character in name: if character! ='.': break level + = 1 if not hasattr (package) 'rindex'): raise ValueError ("' package' not set to a string") dot = len (package) for _ in range (level, 1,-1): try: dot = package.rindex ('.', 0 Dot) except ValueError: raise ValueError ("attempted relative import beyond top-level"package") name = "{}. {}" .format (package [: dot], name [level:]) return LazyModuleType (name) these are all the contents of the article "how to implement module import in Python" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.