| Start |
|---|
| The all special variable Modules can have a special variable named Given the following module: Only However, |
| Importing a module Use the
You can import a module and assign it to a different name: If your python file It is also possible to import a function from a module: To import specific functions deeper down into a module, the dot operator may be used only on the left side of the In python, we have two ways to call function from top level. One is In general But if you are sure enough, in your whole project there is no way having same function name you should use Multiple imports can be made on the same line: The keywords and syntax shown above can also be used in combinations: |
| Import modules from an arbitrary filesystem location If you want to import a module that doesn't already exist as a built-in module in the Python Standard Library (opens new window) nor as a side-package, you can do this by adding the path to the directory where your module is found to It is important that you append the path to the directory in which |
Importing all names from a module for example: This will import all names defined in the Warning: If a function with the same name was already defined or imported, it will be overwritten. Almost always importing only specific names Starred imports are only allowed at the module level. Attempts to perform them in class or function definitions result in a and both fail with: |
| Programmatic importing To import a module through a function call, use the The For older versions of Python, use the Use the functions Taken from standard library documentation (opens new window) Do NOT use |
| PEP8 rules for Imports Some recommended PEP8 (opens new window) style guidelines for imports:
|
| Importing specific names from a module Instead of importing the complete module you can import only specified names:
Another example below (similar to the one above): The following example will raise an error, because we haven't imported a module: Outputs: The python interpreter does not understand what you mean with |
Importing submodules This imports |
| import() function The This function can also be used to specify the file path to a module |
| Re-importing a module When using the interactive interpreter, you might want to reload a module. This can be useful if you're editing a module and want to import the newest version, or if you've monkey-patched an element of an existing module and want to revert your changes. Note that you can't just This is because the interpreter registers every module you import. And when you try to reimport a module, the interpreter sees it in the register and does nothing. So the hard way to reimport is to use But there is more a straightforward and simple way. Python 2Use the Python 3The Syntax
RemarksImporting a module will make Python evaluate all top-level code in this module so it learns all the functions, classes, and variables that the module contains. When you want a module of yours to be imported somewhere else, be careful with your top-level code, and encapsulate it into |
Read Article
https://develop.horje.com/learn/1434/reference