Modules and Packages
As we all know, in the process of computer program development, as the program code more write more, so that the code will be less easy to maintain.
Sometimes, in order to maintain code, the functions of different functions are placed in different xx.py files.
In Python, a. py file is called a module.
What are the benefits of using modules?
1, to provide code maintainability.
2, can avoid the function of the name and variable name conflict. If the two identical function names are in two modules, the direct reference can be directly displayed by calling Xx.function.
But what if different people write module names that are the same? To solve the same module name, Python also refers to the concept of a package.
In Python, a package code means a folder. If a folder needs to be defined as a package, the folder must contain the __init__.py file.
As an example:
Defines a package_test package with the Math module (Python built-in also has the Math module).
Then, when referencing the math module, the
Import Package_test.math
Import Module
Import the system module, the keyword is import.
Import Math
1. Sometimes it is also desirable to import only one function of the math module, without requiring all functions to be imported
from Import Sin,cos
2. If there are two module functions with the same name, the module. function name () shows the call
Import Math,mylog Print Math.log (5)print mylog.log (5,'default')
3. The function name conflict can also be avoided by giving the function an ' alias '
from Import Log from Import Log as Logger Print log (5)print logger (5,'default')
Using __future__
Python already has a 3.x version, so if you want to reference python3.x in the version python2.7.x version, use __future__
>>> 10/33
In Python version 2.7.x, Division gets an integer.
and Python version 3.x. The
>>> 10/33.3333333333333335>>>10//33
Older versions refer to the previous feature in the new version, the
from __future__ Import Division>>> 10/33.3333333333333335
Installing third-party modules
After all, Python's built-in is just some simple modules. If you want to refer to some advanced modules, install them first.
Currently installing third-party modules is more commonly used in the PIP tool.
Open a command prompt: Enter PIP, then pip install + module name (example:pip install Pillow)
Import
>>>fromimport Image
Summary: Mainly explain the concept of modules and packages, and how to import modules, the installation of third-party modules, __future__ role.
Python Learning day 13th-modules and packages