1. Concept of module and package 1.1
Module: After a lot of code, it is not easy to maintain in a. py file, splitting the code into multiple. py files, each of which is a module.
Package: After more modules, it is also easy to duplicate the name, each development module can be put into different packages, referring to the use of package names. Module name.
1.2 Advantages
Variables of the same variable name do not affect each other in different modules
Modules with the same module name do not affect each other in different packages
1.3 Reference Examples
# p1-> Package name,util-> module name
>>> Import P1.util
>>> print P1.UTIL.F (2,10)
1.4 How to differentiate between packages and normal folders
In a file system, a package is a folder;
In Python, each package must have a __init__.py file below it;
__init__.py file Even if it is empty, there should be.
2. Import of Modules 2.1 Direct import
Import Math
>>> Math.pow (2, 0.5) # POW is a function
1.4142135623730951
>>> Math.PI # pi is a variable
3.141592653589793
2.2 Using from ... import xxx
From math import pow, sin, log
>>> Pow (2, ten)
1024.0
>>> sin (3.14)
0.0015926529164868282
2.3 Method/variable conflict issues in imported modules
Direct import will not have a conflict problem
Use the From ... import xxx If there is a conflict, you can use the alias way
From Math import log
The log of from logging import log as Logger # logging now becomes logger
Print log (10) # is called the log of math
Logger (' Import from Logging ') # called the Log of logging
3. Dynamic Import Module 3.1 background
If the imported module does not exist, the Python interpreter will report a importerror error;
In some cases, two different modules provide the same functionality
3.2 principle
Using Importerror error, we can implement dynamic import module through exception handling try-except.
Another,import xxx as XXX can dynamically import modules of the same name for different use names
3.3 Examples
Try
From Cstringio import Stringio
Except Importerror:
From Stringio import Stringio
Or
Try
Import Simplejson as JSON
Except Importerror:
Import JSON
4.__FUTURE__ trial new features 4.1 background
New versions of Python introduce new features, but in fact these features already exist in the previous version. To "try out" a new feature, you can do so by importing some of the features of the __future__ module.
4.2 Examples
# Python 2.7 Integer division result is an integer, Python 3.x has improved the division of integers, "/" In addition to get floating point number, "//" is still an integer
>>> Print 10/3
3
>>> from __future__ Import Division
>>> Print 10/3
3.33333333333
# in Python 3.x, string unification is Unicode, no prefix u is required, and str stored in bytes must prefix b
>>> from __future__ import unicode_literals
>>> s = ' am I an Unicode? '
>>> Print isinstance (S, Unicode)
True
5. Install the third-party module 5.1easy_install5.2 python built-in Pip (already built into 2.7.9)
Select Pip function When installing
Once the installation is complete, you can install web.py this third-party module by using PIP install web.py on the command line
When the third-party module is installed, use the import Web to work properly
Third-party modules looking for: Https://pypi.python.org/pypi
5.3python third-party modules
Http://www.cnblogs.com/mmbbflyer/p/5787494.html
Http://www.cnblogs.com/tester-l/p/5735230.html
Python Advanced--02 How to use the module