Article From:https://www.cnblogs.com/Python1234/p/9121544.html

Summary

Module Brief

Python
__name__

Module advantages

  • Improve the maintainability of the code
  • The code is increased, and when a module is finished, it can be quoted in many places.
  • The conflict of function names and variable names can be avoided

 

  • argv : Get a list of command line parameters
  • import sys Introducing the sys.py module in the python standard library
  • import : This is a method of introducing a module (mentioned later)
  • sys.path : Contains a Python interpreter to automatically find the list of required modules.

Custom module

  • Python Not only can we use third party modules and system modules, but we can also use custom modules.
  • In Python, a.Py file is a module
  • The following is my custom Titan.py module. The code is as follows

 

It is important to note that:

  • A module will only be imported once, no matter how many times you perform import, you can prevent the module from being duplicated.
  • Introduce any module (including custom module) without adding.Py suffix.
  • When we use the import statement, how do the Python interpreters find the corresponding files?
  • This involves the search path of Python, which is made up of a series of directory names, and the Python interpreter searches for the introduced modules from these directories in turn.
  • It looks like environmental variables. In fact, you can define the search path by defining the environment variables.
  • The search path is determined when the Python is compiled or installed, and the installation of the new library should also be modified. The search path is stored in the path variable in the sys module.

Use examples

 

It is important to note that:

This approach may lead to duplicate names or methods defined in this document. Here we need to pay attention.

from…import* mode

Pour all the contents of a module into the current namespace, but it is best not to use it too much.

# Format:From modname import *Use:From Titan import *SayGood ()Print (age)

Module built-in properties and functions

__name__ attribute

.py
__name__
if __name__ == '__main__':
 print('This is the Titan module ')Else:Def sayGood ():Print ('good')Def sayNice ():Print ('nice')Def sayBad ():PrinT ('bad')Age = 20Name ='titan'
  • name And main are both underlined
  • Each module has a __name__ attribute. When its value is equal to __main__, it indicates that the module itself is executing, otherwise it will be introduced into other files.
  • If the current file is the entry file for the program, the value of the __name__ attribute is __main__.

dir() function

dir()
dir()
import Titan
print(dir(Titan))
# Output results:['__builtins__','__cached__','__doc__','__file__','__loader__','__name__','__package__','__spec__','age','name','sayBad','sayGood','sayNice']Print (dir ())Output results:['Titan','__annotaTions__','__builtins__','__cached__','__doc__','__file__','__loader__','__name__','__package_'__spec__']Define a new variable hereSum = 30Print (dir ())Output results:['Titan','__annotations__','__builtins_'sum'],'__cached__','__doc__','__file__','__loader__','__name__','__package__','__spec__','sum']After deleting the defined variablesDel sumPrint (dir ())Output results:['Titan','__annotations__','__builtins__','__cached__','__doc__','__file__','__loader__','__name__','__package__','__spec__']

package

Here we think about the problem: two or more developers in the same project define modules of the same name respectively (of course, in a different directory, the same name is not possible to create a file in the same directory), then what is the call of the module

Solve

  • In order to solve the conflict of module naming, a method of organizing modules by directory is introduced.
  • A package is a form of managing Python module namespaces, using the “dot module name”.
  • After the package is introduced, as long as the top level package does not conflict with other people, then the module will not conflict with others.
  • The top level package refers to the upper level file directory.
  • The same package can contain multiple modules
  • For example, a module called A.B represents a submodule named B in the package named A.

It is important to note that:

In each package (in the module’s same level directory) you have to create a file named __init__.py, which is mainly to avoid a number of names, and at present nothing can be written in the file, as shown in the figure:

 

Invocation method

import a.Titan
import b.Titan
import b.coder
a.Titan.sayGood()
b.Titan.sayGood()
b.coder.sayGood()
# Output results:Good--aGood--bGood--coder

Installation and use of third party modules

pip brief introduction

  • In Python, the third party library is installed and managed through pip. Pip, like pod in iOS, is responsible for installing and managing third party libraries.
  • In Mac and Linux systems, PIP is installed by default. There is no need to install it again. If there are any problems, please solve it by Baidu.
  • There will be a default Python2.7 version in the Mac system. I installed a 3.6 version myself, and naturally installed pip on default.
  • Here are some commands for PIP in Python3.6, where PIP3 is executed as command ends.
# View the current PIP versionPIP3 -VInstallation of third party librariesPIP3 install...PIP upgradePIP3 install --upgrade PIP3

Installation of third party libraries

  • Mac The system installs the third party library, directly opens the terminal to carry out the installation command.
  • Pillow The Python platform is actually a standard library for image processing.
  • PIL The function is very powerful, but API is very simple and easy to use
pip3 install Pillow

The use of third party modules

Operation image

Here is the most common example of image scaling operation code.

from PIL import Image
# Open a JPG image file. Note that the current path is:Im = Image.open ('titan.jpg')Obtain image sizeW, H = im.sizePrint ('image size:%sx%s')% (W, H))Zoom to 50%:Im.thumbnail (((w//2, h//2))Print ('image to:%sx%s'% (w//2, h//2))The zoomed image is used in JPEG format is saved:Im.save ('jun.jpg','jpeg')

Other functions such as slicing, rotation, filter, output text, palette and so on are available. The code is as follows:

from PIL import Image, ImageFilter
# Open a JPG image file. Note that the current path is:Im = Image.open ('jun.jpg')The application of fuzzy filter:Im2 = im.filter (ImageFilter.BLUR)Im2.save('jun1.jpg','jpeg')

Welcome to my public address or blog Garden: Python learning to exchange https://home.cnblogs.com/u/Python1234/

Welcome to join my thousand people learning group: 125240963

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *