-
Notifications
You must be signed in to change notification settings - Fork 3
Packages
A Python package is a folder containing a collection of modules that are related to each other.
Python code is organized in files, in fact, write the entire code in a single file is a way of organizing the code.
Other way to organize the code is create functions for that code that will be repeated along the program. When write a function that can be used anywhere, naming properly to give sense to the code and finally calling that function over and over.
Classes are a way to organize the code in a way that is more readable and easy to understand. From classes we can create objects, from where we can instantiate new data.
As a program gets longer, it may be split in different files for easier maintenance. There is a way to link all of these files together in modules. Each one of the .py files is a module, and using the import statements between modules we are able to use the code from one module to another.
Definitions from a module can be imported into other modules or into the main function to reutilize the code, to avoid the repetition of code and easier maintenance.
A module is a file containing Python definitions and statements, that can be considered to be the same as a code library. The file name is the module name with the suffix .py appended.
Within a module, the module’s name (as a string) is available as the value of the global variable name.
To import a module, use the import statement:
import module_name
This does not enter the names of the functions defined in module_name directly in the current symbol table; it only enters the module name module_name there. Using the module name can be accessed the functions defined in the module by using the dot operator:
module_name.function_name()
A module can contain executable statements as well as definitions and functions. These statements are intended to initialize the module. They are executed only the first time the module name is encountered in an import statement or if the file is executed as a script.
Each moduel has its own private symbol table. This symbol table is used as the global symbol table by all functions defined in the module.
For this reason the author of a module can use global variables in the module without worryng about accidental clashes with a user's global variables.
Modules can import other modules. It is customary but not required to place all import statements at the beginning of a module (or script, for that matter). The imported module names are placed in the importing module’s global symbol table.
There is a variant of the import statement that imports names from a module directly into the importing module’s symbol table.
from module_name import name1, name2
If the module name is followed by as, then the name following as is bound directly to the imported module.
from module_name import name1 as new_name1
It can also be used when utilising from with similar effects:
from module_name import name1 as alias1
In Python, the special name main is used for two important constructs:
-
the name of the top-level environment of the program, which can be checked using the name == 'main' expression;
-
the main.py file in Python packages.
Both of these mechanisms are related to Python modules; how users interact with them and how they interact with each other.
If a module is executed in the top-level code environment, its name is set to the string 'main'.
Before executing code, Python interpreter reads source file and define few special variables/global variables. If the python interpreter is running that module (the source file) as the main program, it sets the special name variable to have a value “main”. If this file is being imported from another module, name will be set to the module’s name.
Use the next code to ensure that the module is executed at the top level and only execute a given function or statement if the module is run as the main program using:
if __name__ == "__main__":
This is a good practice to avoid executing code in a module when it is imported and only run code of the main file.
Packages are a way of structuring Python’s module namespace by using “dotted module names”. Packages is simply a folder containing modules. The way to importa a package is to use the import statement: First call the package name, then the module name.
import package_name.module_name
- notice the dot between package and module names
Each folder must contain the magic file __init__.py
to be considered a package by the interpreter.
Modules intended for use as the main module of a Python application must always use absolute imports.
You can use multiple packages and packages inside packages.
import package_name.subpackage.module_name
from
statements can be used to import names from a module or package.
from package_name.subpackage import module_name
You can be specifit to import a specific name from a module or package. Importing a module from a package, a subpackage, or a module or function with or without an alias given by the as keyword.
__main__
is a special name that is used to identify the main module of a Python program.
wiki footer
custom side bar