PYTHON0.13-----Modules/Packages Overview

Source: Internet
Author: User

Module:
With more and more code in a file, the code becomes more and more difficult to maintain, and the file is basically obsolete. In order to solve the problem of difficult to maintain files, many similar functions can be grouped into separate files, so that each file contains relatively little content, and the approximate function of each file can be represented by file name. Many programming languages do so to organize the code structure. A. py file is a module.

Advantages of this multi-module form:
1. Improve the maintainable type of the code. Because each module corresponds to a function, if the test of which function error, directly can find the corresponding module.
2. Improved code reuse. Because a module inside a single function, it will be needed in many places, can be referenced in multiple places. If the module is too large and the content is too miscellaneous, then the likelihood of being required by other programs is greatly reduced.
3. Avoid conflicts between the function name and the variable name. As the file grows, the content of each module is reduced, and the probability of the function and variable having the same name becomes smaller and less prone to collisions.

4. They can also refer to other modules (built-in modules, three-party modules, custom modules):

Built-in modules: Systems already included. For example: OS, Math,random,time.
Three-party module: A module that someone else has written.
Custom modules: The modules you write yourself.

Use the modules in the standard library:
Import SYS #系统模块
Common SYS properties:

SYS.ARGV Properties:

When Python executes to a script, for example: Python test.py
If you enter in the cmd window: Python test.py a b C
Then sys.argv[0]= ' test.py '
Sys.argv[1]= ' a '
Sys.argv[2]= ' B '
Sys.argv[3]= ' C '
These argv variables can be accessed within the test.py file.

Sys.path property: Automatically finds the list of required module paths.
My computer from top to bottom of the path order is:
' C:\\users\\yuliang\\desktop\\project ',
' E:\\python3\\python36.zip ',
' E:\\python3\\dlls ',
' E:\\python3\\lib ',
' E:\\python3 ',
' E:\\python3\\lib\\site-packages ',
' E:\\python3\\lib\\site-packages\\win32 ',
' E:\\python3\\lib\\site-packages\\win32\\lib ',
' E:\\python3\\lib\\site-packages\\pythonwin ']

Three ways to introduce modules :
For example: A. py file test.py is already defined, and the name of this custom module is test.
def sayname (name):
print (' Your name is%s '%name)
def sayage (age)
print (' You are%d '%age)
def sayfavor (Favor):
Print (' Favor is%s '%favor)


first:
Span style= "font-family:"microsoft yahei"; Font-size:16px "> #若有其它. PY module requires this test module, you can copy the test.py file to the same folder as the. py file.
Introducing the word definition module in other modules test:
format: Import module1[,module2[,module3[ ..., Modulen]]]
example: Import Time,random,os
import test.py #一个模块只会被引入一次, no matter how many times import is executed. Prevents the module from being introduced repeatedly.

Use the contents of the module:
Format: module name. function/Variable name
Example: Test.sayname (' Bob ')

The second type:
From .... import Statement
#从模块中导入一个指定的部分到命名空间
format: From Module1 import name1[,name2[,...... Namen]]
For example:
From test import sayname,sayage
sayname (' Bob ') #这种引入方式不用写模块名
sayage #相当于把这两个函数定义到本. py file.
Sayfavor (' basketball ') #没有引入这个函数, will error.
Note: Functions in the program content can overwrite functions with the same name in the module.

third:
Span style= "font-family:"microsoft yahei"; FONT-SIZE:16PX ">from Module Import * (deprecated)
#作用: all the contents of a module are imported into the current namespace.
Sayname (' Bob ')
Span style= "font-family:"microsoft yahei"; Font-size:16px "> Sayage (+)
Sayfavor (' basketball ') #都不会报错.
Note: Functions in the program content can overwrite functions with the same name in the module.

__NAME__ Properties:
#模块就是一个. py file, a module is introduced by another program. If you do not want some code in the module to execute, you can use the __name__ property to make the program call only part of the module. When __name__= ' __main__ ', indicates that the module itself is executing, and the current file is the entry file for the program. If the file is used as a module, then __name__= ' module name '

Package:
If different people write modules with the same name, in order to resolve conflicts with the same name of the module, the method of organizing modules by directory is introduced, called packages. If the directory is the same, set a different name for the directory.
features : After the package is introduced, the modules do not conflict with each other as long as the top-level package does not conflict with the other packages.


For example: Use a custom package:
There's a file called test.py.
The content is: Def say ():
Print (' Lalala ')
Put this test.py on the outside of the bread a folder, in the A folder under the new __init__.py file (the file does not need to add anything, add this __init__.py file is to let Python recognize this is a package, to prevent some impostors file is mistaken for the package), Then a file is a package.

There's another file called test.py.
The content is: Def say ():
Print (' Pipipi ')
Put this test.py on the outer Bread B folder, under the B folder to create a new __init__.py file (the file does not need to add anything, add this __init__.py file is to let Python recognize this is a package, to prevent some impostors file is mistaken for the package), Then the B file is a package.
Note : If the A folder and the B folder have the same name, then A/a folder is set up with a folder of the same name, of course, plus __init__.py

Introduction Package:
Import A.test
Import B.test
A.test.say ()
B.test.say ()

Download third-party packages:
Windows: Tick the PIP and add Python to path and automatically configure the environment variables.
Mac,windows comes with Python, no installation required.
#要安装三方模块, you need to know the name of the module.
Example: Pip install Pillow #表示安装模块Pillow (powerful image processing tool library)
#windows如果报错, enter pip Insatll--upgrade pip
Add: Linux,mac also exist 2.x and 3.x versions, input PYTHON,PIP default to 2.x version, if you want to use 3.x version, you should use the directive Python2 and PIP3

PYTHON0.13-----Modules/Packages Overview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.