The usage of import in python
In Python, the import statement is used to bring in functions, classes, and variables defined in other Python modules (files) so that they can be used in the current file.
Here are some common ways to use the “import” statement:
- Import the entire module:
- use module_name.
- You can use the functions, classes, and variables defined in the module_name module in the current file.
- Import specific functions, classes, or variables:
- import name1, name2, … from the module named module_name
- Directly import name1, name2, etc. from module_name module into the current file namespace, so that you can use these imported objects without requiring the module name prefix to access them.
- Import the entire module and give it an alias:
- import the module named “module_name” and give it the alias “alias_name”
- Import the module named module_name into the current file namespace, give it the name alias_name, and use alias_name to access objects in the module.
- Import specific functions, classes, or variables and give them an alias:
- Import specific elements (name1, name2, etc.) from module_name and provide them with aliases (alias1, alias2, etc.).
- Import name1, name2, etc. directly from the module_name module into the current file namespace, and name them respectively as alias1, alias2, etc.
- Importing all objects from a module (not recommended).
- import everything from module_name
- Import all functions, classes, and variables from the module_name module directly into the current file’s namespace, allowing for direct use of these imported objects but potentially causing naming conflicts.
Note: When using the import statement to import modules, Python will search for the specified module, first within the built-in modules, and then within the directories specified in the sys.path list.