Notice: While JavaScript is not essential for this website, your interaction with the content will be limited. Please turn JavaScript on for the full experience.
...import spam from .moduleY import spam as ham from . import moduleY from ..subpackage1 import moduleY from ..subpackage2.moduleZ import eggs from ..moduleA import foo from ...package import bar from ...sys import path Note that while that last case is legal, it is certainly discouraged ("insane" was the word Guido used). Relative imports must always use from <> import; import <> is always absolute. Of course, absolute imports can use from <> import by omitting the leading dots...
...imported (possibly running its initialization code, __init__.py) and then imports whatever names are defined in the package. This includes any names defined (and submodules explicitly loaded) by __init__.py. It also includes any submodules of the package that were explicitly loaded by previous import statements, e.g. import Sound.Effects.echo import Sound.Effects.surround from Sound.Effects import * In this example, the echo and surround modules are imported in the curren...
...import.c, and makes lookup easy. This same mechanism is used to speed up directory (non-zip) imports. See below. zlib Compressed zip archives require zlib for decompression. Prior to any other imports, we attempt an import of zlib. Import of compressed files will fail with a message "missing zlib" unless zlib is available. Booting Python imports site.py itself, and this imports os, nt, ntpath, stat, and UserDict. It also imports sitecustomize.py which may import more modules. Zip imports...
...import mechanism. When an import statement is encountered, the interpreter looks up the __import__ function in the built-in name space. __import__ is then called with four arguments, amongst which are the name of the module being imported (may be a dotted name) and a reference to the current global namespace. The built-in __import__ function (known as PyImport_ImportModuleEx() in import.c) will then check to see whether the module doing the import is a package or a submodule of a package. If i...
...import a "pure virtual" package (like zc), we allow only importing of the contents of virtual packages. That is, a statement like import zc should raise ImportError if there is no zc.py or zc/__init__.py on sys.path. But, doing import zc.buildout should still succeed, as long as there's a zc/buildout.py or zc/buildout/__init__.py on sys.path. In other words, we don't allow pure virtual packages to be imported directly, only modules and self-contained packages. (This is an acceptable limitation...
...import__() function. The import of a module will proceed using the state stored in the ImportEngine instance rather than the global import state. For full documentation of __import__ functionality, see [2] . __import__() from ImportEngine and its subclasses can be used to customise the behaviour of the import statement by replacing __builtin__.__import__ with ImportEngine().__import__. import_module(name, package=None) A reimplementation of importlib.import_module() which uses the import state ...
...ImportError("Module %s has not been imported" % (name,)) if module.__notified__: return try: module.__notified__ = True if '.' in name: notify(name[:name.rfind('.')]) for callback in post_import_hooks[name]: callback(module) finally: post_import_hooks[name] = None XXX C API New C API functions PyObject* PyImport_GetPostImportHooks(void) Returns the dict sys.post_import_hooks or NULL PyObject* PyI...
...imports from __main__ in order to run correctly under Python 3. While such scripts could be imported in Python 2 (due to implicit relative imports) it is already the case that they cannot be imported in Python 3, as implicit relative imports are no longer permitted when a module is imported. By disallowing implicit relatives imports from the main module as well, such modules won't even work as scripts with this PEP. Switching them over to explicit relative imports will then get them working agai...
...importable. This approach also has the same disadvantage as the use of absolute imports of sibling modules - if the script is moved to a different package or subpackage, the boilerplate will need to be updated manually. It has the advantage that this change need only be made once per file, regardless of the number of relative imports. Note that setting __package__ to the empty string explicitly is permitted, and has the effect of disabling all relative imports from that module (since the import ...
...import os as real_os from os import fdopen as os_fdopen, exit, stat as os_stat The as name is not intended to be a keyword, and some trickery has to be used to convince the CPython parser it isn't one. For more advanced parsers/tokenizers, however, this should not be a problem. A slightly special case exists for importing sub-modules. The statement import os.path stores the module os locally as os, so that the imported submodule path is accessible as os.path. As a result, import os.path a...
...importlib.abc.MetaPathFinder.find_module() importlib.abc.PathEntryFinder.find_module() importlib.abc.PathEntryFinder.find_loader() importlib.abc.Loader.load_module() importlib.abc.Loader.module_repr() importlib.util.set_package() importlib.util.set_loader() importlib.find_loader() Removals These were introduced prior to Python 3.4's release, so they can simply be removed. importlib.abc.Loader.init_module_attrs() importlib.util.module_to_load() Other Changes The import system implementati...
...importing the containing package and using the PEP 302 import hooks to gain access to the Python code. Note that the process of locating the module to be executed may require importing the containing package. The effects of such a package import that will be visible to the executed module are: the containing package will be in sys.modules any external effects of the package initialisation (e.g. installed import hooks, loggers, atexit handlers, etc.) Reference Implementation A reference impl...
...imports became dependent on the __name__ attribute of the module performing the import. This is because the use of dots in a relative import are used to strip away parts of the calling module's name to calculate where in the package hierarchy an import should fall (prior to PEP 328 relative imports could fail and would fall back on absolute imports which had a chance of succeeding). For instance, consider the import from .. import spam made from the bacon.ham.beans module (bacon.ham.beans is no...
...imported by other modules, it would be deleted first by step M4 anyway, but if it is imported elsewhere, deleting __main__ is a plausible way to break a tie. M4 is an explicit garbage collection loop -- it deletes all those modules which are referenced by no other modules, only by the table of modules (sys.modules) itself. It may not delete all modules, however, when there are mutual imports; the remaining steps take care of those. M5 is needed to take care of mutually recursive impo...
...imported, because project2 was added to sys.path: >>> import parent.child.two # we cannot import parent.child.three, because project3 is not in the path: >>> import parent.child.three Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<frozen importlib._bootstrap>", line 1286, in _find_and_load File "<frozen importlib._bootstrap>", line 1250, in _find_and_load_unlocked ImportError: No module named 'parent.child.three' # ...
...import extend_path __path__ = extend_path(__path__, __name__) in the package's __init__.py. Every distribution needs to provide the same contents in its __init__.py, so that extend_path is invoked independent of which portion of the package gets imported first. As a consequence, the package's __init__.py cannot practically define any names as it depends on the order of the package fragments on sys.path which portion is imported first. As a special feature, extend_path reads files named <pack...
...import to fail, because any existing non-exceptional Windows import finds a case-sensitive match first in the path -- and it still will. An exceptional Windows import currently blows up with a NameError or ImportError, in which latter case it still will, or in which former case will continue searching, and either succeed or blow up with an ImportError. #A is needed to cater to case-destroying filesystems mounted on Windows, and may also be used by people so enamored of "natural" Windows behavio...
...import statement: there is a standard module __future__.py, described later, and it will be imported in the usual way at the time the future_statement is executed. The interesting runtime semantics depend on the specific feature(s) "imported" by the future_statement(s) appearing in the module. Note that there is nothing special about the statement: import __future__ [as name] That is not a future_statement; it's an ordinary import statement, with no special semantics or syntax restrictions. ...
...imported under its official name module.name, and therefore that if the program again imports that name then it will obtain the same module instance. That actuality is that the module was imported only as '__main__'. Another import will obtain a distinct module instance, which can lead to confusing bugs, all stemming from having two instances of module global objects: one in each module. Examples include: module level data structures Some modules provide features such as caches or registries as...
...Import statement: import name, import module as name, from module import name Implicit assignment: names are bound by for statements and except clauses There are several cases where Python statements are illegal when used in conjunction with nested scopes that contain free variables. If a variable is referenced in an enclosed scope, it is an error to delete the name. The compiler will raise a SyntaxError for 'del name'. If the wild card form of import (import *) is used in a function a...