Python Modules Tutorial For Beginners 2019. Here in this blog post Coding compiler sharing Python 3 modules programming tutorial for beginners. You will learn about Python Modules and Packages. This Python tutorial is for absolute beginners who are looking for learning Python programming. Let’s start learning Python 3.
If you exit the Python interpreter and re-enter, any definitions (variables and methods) you make will be lost. Therefore, if you want to write a larger program, it would be better to use a text editor for preparing the interpreter input, and execute that file as an input instead.
This is the legend of the script . As your program grows longer, you may want to split it into several more maintainable files. You may also want to use handy functions in different programs instead of copying the code between them.
To meet these needs, Python provides a way to get definitions from a file and use it in an interactive instance of a script or interpreter. Such files are referred to as the module ; defining module may be introduced into another module or the main module in (variable set when a script executed at the top level, and in calculator mode).
Modules are files that include Python definitions and declarations. File name is the module name with
.pythe suffix. Module module name (as a string) can be a global variable
__name__to get. For example, you can use your favorite file editor to create a file named fibo.py in the current directory. Enter the following:
# Fibonacci numbers module Def fib ( n ): # write Fibonacci series up to n a , b = 0 , 1 while b < n : print ( b , end = '' ) a , b = b , a + b print () Def fib2 ( n ): # return Fibonacci series up to n result =  a , b = 0 , 1 while b < n : result . append ( b ) a , b = b , a + b return result
Now enter the Python interpreter and import this module using the following command:
>>> import fibo
This does not directly
fiboin the current function import symbol table; it only enters the module name
fibo. You can access this function by the module name as follows:
>>> fibo . fib ( 1000 ) 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 >>> fibo . fib2 ( 100 ) [1, 1, 2, 3, 5, 8, 13, 21 , 34, 55, 89] >>> fibo . __name__ 'fibo'
If you plan to use a function frequently, you can give it a local variable:
>>> fib = fibo . fib >>> fib ( 500 ) 1 1 2 3 5 8 13 21 34 55 89 144 233 377
1. In-depth module
In addition to including function definitions, modules can also contain executable statements. These statements are generally used to initialize the module. They only in the first execution of a place to be imported.
Each module has its own private symbol table, which is used as a global symbol table by all function definitions in the module. Therefore, the module’s author can use global variables inside the module without worrying about accidentally colliding with a user’s global variables.
On the other hand, if you know exactly what you are doing, you can use the notation of a referenced module function to access the module’s global variables
Related Article: Python Interpreter Tutorial
Modules can import other modules. A (good) habit is to place all import statements at the beginning (or script) of the module. This is not mandatory. The imported module name is placed in the global symbol table of the current module.
A variant of the import statement imports the semantic table named into this module directly from the imported module. E.g:
>>> from fibo import fib , fib2 >>> fib ( 500 ) 1 1 2 3 5 8 13 21 34 55 89 144 233 377
This will not import the module name from the local semantics table (as shown above,
There are even ways to import all the definitions in the module:
>>> from fibo import * >>> fib ( 500 ) 1 1 2 3 5 8 13 21 34 55 89 144 233 377
This will import all
_names that begin with an underscore ( ).
Note that in practice often do not encourage the use of a module or a package from
*import all, because it makes the code becomes difficult to read. However, this is convenient and effortless in an interactive session.
Note: For performance reasons, each module only imports once in each interpreter session. Therefore, if you modify your module, you need to restart the interpreter; or, if you want to test such a module interactively, reload it with imp.reload() , for example .
import imp; imp.reload(modulename)
1.1. Executing a Module as a Script
When you run a Python module in the following way, the code in the module is executed:
Python fibo . py < arguments >
Module code will be executed, just imported it, but this time
"__main__". This is equivalent to if you add the following code after the module:
If __name__ == "__main__" : import sys fib ( int ( sys . argv [ 1 ]))
You can have this file behave like a script when it is imported as a module. This code is only called when the module is executed as a “main” file:
$ python fibo.py 50 1 1 2 3 5 8 13 21 34
If the module is imported, this code will not be executed:
>>> import fibo >>>
This is usually used to provide the module with a user-friendly interface (using the module as a script to perform test requirements).
Related Articles: Learn Python In One Day
1.2. Module’s search path
Importing a call
spamwhen the modules, the interpreter named first search in the current directory
spam.pyfiles. If it is not found, then it will be looked up in the directory list given in the sys.pathvariable. The initial value of the sys.path variable comes from the following:
Enter the directory of the script (current directory).
Search the directory list represented by the environment variable PYTHONPATH
(This and shell variables
PATHHas the same syntax, ie a list of a series of directory names).
Python searches in the default installation path.
Note : In a file system that supports symbolic links, the directory in which the script is entered is the directory to which the symbolic link points. In other words, directories containing symbolic links are not added to the directory search path.
In fact, the interpreter searches the module for the path directory specified by the sys.path variable. This variable is initialized by default to include the input script (or current directory), PYTHONPATH,and the installation directory. This allows Python programs to understand how to modify or replace module search directories.
It should be noted that since these directories contain scripts that are run in the search path, these scripts should not be renamed with standard modules, otherwise Python will try to load these scripts as modules when importing modules. This usually throws an error. See standard module for more information.
1.3. “Compiled” Python Files
In order to speed up the load module, Python will be in
__pycache__the following directory names cached version of each module compiled version here after the preparation of the format of the file compiled. It usually contains the Python version number.
For example, in CPython version 3.3, the compiled version of spam.py is cached as . This naming convention allows modules that are compiled from different releases and different versions of Python to exist at the same time.
Python checks the modified date of the source and compiled versions to determine if it is out of date and needs to be recompiled. This is a completely automated process. At the same time, the compiled modules are cross-platform, so the same library can be shared between systems of different architectures.
Python does not check the cache in two different environments. First, it will always recompile and it will not store modules loaded directly from the command line. Second, it does not check the cache if there is no source module. To support the release of no source files (only compiled versions), the compiled module must be in the source directory and must have no source file module.
Related Article: Python For Machine Learning
Some advanced tips:
To reduce the size of a compilation module, you can use -O or -OO on the Python command line . The -O parameter deletes the assert statement, and the -OO parameter removes the assert statement and the __doc__ string.
Because some programs rely on the availability of these variables, you should only use this option if it is correct. “Optimized” modules have a .pyo suffix instead of a .pyc suffix. Future versions may change the effect of optimization.
.pyoprogram file does not come from more than
.pya faster run-time files;
.pyofiles is the speed with which they are loaded.
There are many details about this part in PEP 3147, and it contains a decision process.
2. Standard Module
Python comes with a standard module library and publishes a separate document called the Python Library Reference Manual (hereafter referred to as the “Library Reference Manual”).
Some modules are built into the interpreter. The access interface for these operations is not part of the language kernel but is already built into the interpreter. This is both to increase efficiency and to provide interfaces for operating system native access such as system calls.
This collection of modules is a configuration option that depends on the underlying platform. For example, the winreg module is only available on Windows systems.
There is a specific module worth noting: sys , this module is built into all Python interpreters. Variable
sys.ps2defines the primary and secondary prompt string prompt:
>>> import sys >>> sys . ps1 '>>> ' >>> sys . ps2 '... ' >>> sys . ps1 = 'C>' C> print('Yuck!') Yuck! C >
These two variables are only meaningful in the interactive mode of the interpreter.
sys.pathis the list of strings the interpreter module search path. It consists of an environment variable PYTHONPATH initialization, if not set PYTHONPATH , then the default values built initialization. You can modify it with standard string manipulation:
>>> import sys >>> sys . path . append ( '/ufs/guido/lib/python' )
3. The dir() function
The built-in function dir() is used to search for module definitions by module name, which returns a string-type storage list:
>>> import fibo , sys >>> dir ( fibo ) ['__name__', 'fib', 'fib2'] >>> dir ( sys ) ['__displayhook__', '__doc__', '__excepthook__', '__loader__' , '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe', '_home', '_mercurial', '_xoptions', ' Abiflags', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'Builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencoding', 'getobjects', 'getprofile', 'getrecursionlimit', 'getrefcount ', 'getsizeof', 'getswitchinterval', 'gettotalrefcount', 'gettrace', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'setcheckinterval', 'Setdlopenflags', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions']
When called without parameters, the dir() function returns the currently defined name:
>>> a = [ 1 , 2 , 3 , 4 , 5 ] >>> import fibo >>> fib = fibo . fib >>> dir () ['__builtins__', '__doc__', '__file__', '__name__ ', 'a', 'fib', 'fibo', 'sys']
Note that this list lists all types of names: variables, modules, functions, and so on.
Dir() does not list built-in functions and variable names. If you want to list these, they are defined in the standard module builtins :
>>> import builtins >>> dir ( builtins ) ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', ' ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError' , 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning ', 'PermissionError', 'ProcessLookupError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__build_class__', '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes ', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex ', 'id', 'input', 'int', 'isinstance', 'issubclass', 'Iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open' , 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', ' Sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars',Property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum' , 'super', 'tuple', 'type', 'vars',Property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum' , 'super', 'tuple', 'type', 'vars', 'zip']
Packages are usually structured module namespaces using “dot module names.” For example, the name
A.Bmodule represents called
Bsubmodule. Just as using modules to save different module architectures can avoid conflicting global variables, using dot module names to save different library architectures like NumPy or Python Imaging Library can avoid naming conflicts between modules.
Suppose you now want to design a set of modules (a “package”) to handle sound files and sound data in a unified manner. The presence of several different sound formats (usually recognized by their extension, for example:
.auso, in order to switch between different types of file formats, you need to maintain a growing collection of packages.
Maybe you also want to do a lot of different operations on sound data (such as mixing, adding echo, applying balance functions, creating an artificial effect), so you have to add an infinite stream module to perform these operations. Your package might look like this (grouped by a hierarchical file system):
Sound/ Top-level package __init__.py Initialize the sound package Formats/ Subpackage for file format conversions __init__.py Wavread.py Wavwrite.py Aiffread.py Aiffwrite.py Auread.py Auwrite.py ... Effects/ Subpackage for sound effects __init__.py Echo.py Surround.py Reverse.py ... Filters/ Subpackage for filters __init__.py Equalizer.py Vocoder.py Karaoke.py ...
When importing the package, Python through
sys.pathto find subdirectory contains the package search path.
To make the directory as a Python package contents, it must be included in the directory
__init__.pyfile. This is to avoid that a directory with a bad name inadvertently hides valid modules that appear later in the module search path, such as string.
In the simplest case, only an empty
__init__.pyfile. Of course, it can also execute initialization code for the package described later or define
Users can import only specific modules in the package at a time, for example:
This includes any
sound.effects.echosub-modules. It must be referenced by its full name:
Sound . Effects . echo . echofilter ( INPUT , Output , Delay = 0.7 , ATTEN = . 4 )
There is an alternative way to import packages:
From sound.effects import echo
echothe sub-module, and so that it can be used without the package prefix case, it can be called in the following manner:
Echo . echofilter ( input , output , delay = 0.7 , atten = 4 )
There is another variation for directly importing functions or variables:
From sound.effects.echo import echofilter
This once again loaded
echosub-modules, but so that you can call it a direct
Echofilter ( input , output , delay = 0.7 , atten = 4 )
Note that use when introducing packet mode, the sub-item (item) can be either a sub-module packet (or sub-packets), or may be some other name defined in the package, like a function, class or variable.
The statement first checks whether this subkey exists in the package. If not, it assumes it is a module and tries to load it. If it is not found, an ImportError exception is thrown.
from package import item
In contrast, using a similar when this syntax, each item must be a package, the last item may be a module or a package, but not the class, function or variable defined in the previous item.
4.1. Importing Packages from *
When the user writes What will happen? Ideally, you always want to find all the submodules of a package in the file system and import them. This may take a long time, and there is a border effect beyond expectations, exporting packages that you want to be explicitly imported.
from sound.effects import *
The only solution for the author of the package is to provide a clear package index. import statement uses the following conditions: performing , if the package code defines a named list, will be introduced into the module names given in the list.
The author can update this list at will when the new version of the package is released. If package authors do not want to import all modules in their packages when they import *, they may also decide not to support it (import * ). For example, this file may include the following code:
from package import *
__all__ = [ "echo" , "surround" , "reverse" ]
This means that the statement from import the three named submodules of the package.
from sound.effects import *
If not defined
__all__, the statement is not the import all submodules package. No matter how many named custom packages, the only certainty is introduced into the bag (may run initialization code) and all names defined in the package then imports.
Thus the introduction of each named (and submodules explicitly imported) in. Also included are the submodules that the import statement explicitly imports from the package. Consider the following code:
from sound.effects import *
Import sound.effects.echo import sound.effects.surround from sound.effects import *
In this example,
surroundmodules imported in the current namespace, this is because the execution
from...importtime of statements they have been defined in the
sound.effectspackage in a (defined
__all__also works when).
Although some modules are designed to use only the export name matches a specification / mode when, still do not recommend the use of such an approach in production code.
Remember, no mistakes! In fact, unless the imported module needs to use a submodule with the same name in another package, this is the recommended method.
from Package import specific_submodule
4.2. References in Packages
When packages are structured into subpackages (as in the example
soundpackages), may be introduced from an adjacent sub-module packet absolute position. For example, if
sound.filters.vocoderthe packet requires
echomodule, it can be .
from sound.Effects import echo
You can use this form to write explicit relative position of the import. Those explicit relative imports mark the association with the point number to import the current and the upper package. To module, for example, you can use this:
from module import name
From . import echo from .. import formats from ..filters import equalizer
It should be noted that the explicit or implicit relative position import is based on the current module name. Because the name of the main module is always
"__main__"the main module Python application should always use absolute import.
4.3. Packages in Multiple Catalogs
The package supports a more special feature, __path__ . In the package
__init__.pybefore the file code is executed, the variable is initialized to a list of directory names. This variable can be modified, it acts on the search function of sub-packages and modules in the package.
This feature can be used to expand the set of modules in a package, but it is not commonly used.