Python which module path




















Save Article. Improve Article. Like Article. Last Updated : 09 May, Previous How to move list of folders with subfolders using Python? Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment?

Please use ide. This is an error unless the replacement is intended. See section Standard Modules for more information. For example, in CPython release 3. This naming convention allows compiled modules from different releases and different versions of Python to coexist.

This is a completely automatic process. Also, the compiled modules are platform-independent, so the same library can be shared among systems with different architectures. Python does not check the cache in two circumstances. Second, it does not check the cache if there is no source module. To support a non-source compiled only distribution, the compiled module must be in the source directory, and there must not be a source module.

You can use the -O or -OO switches on the Python command to reduce the size of a compiled module. Future releases may change the effects of optimization. The module compileall can create.

There is more detail on this process, including a flow chart of the decisions, in PEP Some modules are built into the interpreter; these provide access to operations that are not part of the core of the language but are nevertheless built in, either for efficiency or to provide access to operating system primitives such as system calls. The set of such modules is a configuration option which also depends on the underlying platform.

For example, the winreg module is only provided on Windows systems. One particular module deserves some attention: sys , which is built into every Python interpreter. The variables sys. The variable sys. You can modify it using standard list operations:. The built-in function dir is used to find out which names a module defines. It returns a sorted list of strings:. Without arguments, dir lists the names you have defined currently:. If you want a list of those, they are defined in the standard module builtins :.

For example, the module name A. B designates a submodule named B in a package named A. There are many different sound file formats usually recognized by their extension, for example:.

There are also many different operations you might want to perform on sound data such as mixing, adding echo, applying an equalizer function, creating an artificial stereo effect , so in addition you will be writing a never-ending stream of modules to perform these operations. When importing the package, Python searches through the directories on sys. This prevents directories with a common name, such as string , unintentionally hiding valid modules that occur later on the module search path.

This loads the submodule sound. It must be referenced with its full name. This also loads the submodule echo , and makes it available without its package prefix, so it can be used as follows:. Again, this loads the submodule echo , but this makes its function echofilter directly available:.

Note that when using from package import item , the item can be either a submodule or subpackage of the package, or some other name defined in the package, like a function, class or variable. The import statement first tests whether the item is defined in the package; if not, it assumes it is a module and attempts to load it.

If it fails to find it, an ImportError exception is raised. Contrarily, when using syntax like import item. The finder is responsible for locating a module and providing a loader to manage the actual import. Adding a custom module finder is as simple as appending a factory to the sys.

On import, each part of the path is given to a finder until one claims support by not raising ImportError. That finder is then responsible for searching data storage represented by its path entry for named modules. This example illustrates how the finders are instantiated and queried. The NoisyImportFinder raises ImportError when instantiated with a path entry that does not match its special trigger value, which is obviously not a real path on the filesystem.

This test prevents the NoisyImportFinder from breaking imports of real modules. When the finder locates a module, it is responsible for returning a loader capable of importing that module. This example illustrates a custom importer that saves its module contents in a database created by shelve. The first step is to create a script to populate the shelf with a package containing a sub-module and sub-package. A real packaging script would read the contents from the filesystem, but using hard-coded values is sufficient for a simple example like this.

Next, it needs to provide finder and loader classes that know how to look in a shelf for the source of a module or package.

Now ShelveFinder and ShelveLoader can be used to import code from a shelf. For example, importing the package created above:. The shelf is added to the import path the first time an import occurs after the path is modified. The finder recognizes the shelf and returns a loader, which is used for all imports from that shelf.

The initial package-level import creates a new module object and then execs the source loaded from the shelf, using the new module as the namespace so that names defined in the source are preserved as module-level attributes. Reloading a module is handled slightly differently.

Instead of creating a new module object, the existing module is re-used.



0コメント

  • 1000 / 1000