Python Full stack development "12th" Python modules and packages

Source: Internet
Author: User

First, the module

1.import ....

A py file can be used as a module

Module import: Directly import the file name, do not need to take the suffix

Function call in module: module name. function name ()

There are three things to do when importing a module: 1. First a new namespace is opened My_moudle

2. Executed the code within the My_moudle

3. Bind the name and namespace inside the My_moudle.

Note: The module will only be imported once in a program and will not be re-imported (in order to conserve resources) so how to implement the module will only be imported once in the program? (this feature enables single-case mode)

After importing a file, the module will be stored in memory, when the import again, it will be in memory to see if the module was imported, if it has been imported, it is no longer imported. Is through the module method inside the SYS

Import Sysfor i in Sys.modules:  #查看是否导入过这个模块    Print (i)

The imported module has its own namespace (an alias can be created for the imported module, resulting in a namespace that is only associated with aliases)

Import  My_moudle as  mm<br>print (Mm.money)
# alias #mysql.pydef Sqlparse ():    print (' from MySQL sqlparse ') #oracle. Pydef sqlparse ():    print (' from Oracle Sqlparse ') #test. Pydb_type=input (' >>: ') if db_type = = ' MySQL ':    import mysql as dbelif db_type = = ' Oracle ':    

2.from ... import ... (aliases are also supported)

This form imports what can use what, do not import all can not use

The import name is equivalent to the global variable.

From TMP import READ1READ1 () from TMP import Readread ()

Parameter problems and return value problems are the same as functions (where to return where to receive, where to pass the parameter)

# from ... import...from zeze import Read1money = 1000read1 () #-------------------from Zeze import Read2def read1 ():    PR Int (' ========== ') read2 () Read1 ()
#测试: Imported function Read1, Read1 overridden by the current position definition from Zeze import read1def read1 ():    print (' ========== ') read1 ()

One particular point to note is that variable assignment in Python is not a storage operation, but a binding relationship, as follows:

From Zeze import money,read1money=200# binds the name money of the current location to the 200print (money) #打印当前的名字read1 () #读取zeze. PY name money, still 100

From...import *

* In conjunction with all, the first thing in the module is to import all the content that is not ' _ '

You can control what you import through __all__, but only with *

* and __all__ cooperation: __all__[' read1 ', ' read2 '],all inside what, * there is nothing, if not all, are imported into the.

# from My_moudle import *# from my_moudle import _money# print (READ1) # print (read2) # print (_money) # print (read)

3. Execute the module as a script

#mytmp. Pyimport Mokuaiprint (Mokuai.money) # If you want to define the module, like the RE module call, do not show what, when the call is displayed, in order to let TMP also do not display the contents of the inside, In the TMP module, judge if __name__== ' __main__ ': #tmp. Pymoney = 100def read1 ():    print (' Read1 ', money)    return ' Hello ' def Read2 ():    print (' read2 ') if __name__== ' __main__ ':     read1 ()     read2 ()

  

4. Module Search Path

The order in which the modules are found is: modules that are loaded in memory, built-in modules->sys.path paths

Lib inside is built-in module.

Extension modules are typically in site-packages

Sys.path: NOTE: When searching in Sys.path from left to right, the first priority is found, Sys.path may also contain. zip archive files and. egg files, and Python treats. zip archives as a directory.

Do not define these modules or keywords that you are familiar with.

5. Compiling Python files

To increase the speed of the loading module, it is emphasized that the loading speed is not the speed of the operation. The Python interpreter caches the compiled version of each module in the __pycache__ directory, in the format: MODULE.VERSION.PYC. The version number of Python is usually included.

1. The PYc suffix is the compiled file

2. When compiling the PYc file, only do it when the file is imported (that is, he compiles it as a module)

second, the package

1. Whether it is an import or From...import form, it is the first time to be alert when you encounter a dot in an imported statement (rather than in use): This is the import syntax for packages

2. Packages are directory-level (folder-level), folders are used to form a py file (the essence of the package is a directory containing __init__.py files)

3. Import the file, the resulting namespace name from the file, import package, the name of the resulting namespace is also derived from the file, that is, the __init__.py under the package, the import package is the essence of the import of the file

Emphasize:

1. In Python3, even if there is no __init__.py file under the package, import package will still not error, and in Python2, the package must have the file, or import packet error

2. The purpose of creating the package is not to run, but to be imported to use, remember that the package is just a form of the module, the package is a module

Precautions:

1. Import statements related to packages are also divided into imports and from ... import ... Either way, regardless of location, you must follow a principle when importing: any point on the import, the left side must be a package, otherwise illegal. Can carry a series of points, such as Item.subitem.subsubitem, but all must follow this principle.

2. For import, there is no such restriction when used, the left side of the point can be a package, a module, a function, a class (they can all call their own properties in the way of a point).

3. Compare the application scenarios for import item and from item import name:
If we want to use name directly then we must use the latter.

4.import

Import glance.db.modelsglance.db.models.register_models (' MySQL ')

5.__init__.py file:

Either way, the first time the package is imported or any other part of the package, the __init__.py file under the package is executed sequentially (we can verify it by printing a line within each package's file), which can be empty, but it can also hold some code to initialize the package.

6.from glance. API Import *

When we talk about modules, we've talked about importing all from a module, where we're studying importing all * from a package.

Here is to import all from the package API, in fact, the statement will only import the package API under the __init__.py file defined in the name, we can define the __all___ in this file:

7. Absolute path Import and relative path import

Absolute path: Starting with glance

Relative path: With. Or. (can only be used in one package and not in different directories)

The relative path can only be used in packages (with or without). Executed under the module is an error)

In glance/api/version.py #绝对导入from glance.cmd import managemanage.main () #相对导入from: CMD import managemanage.main ()

8. Importing packages separately

Python Full stack development "12th" Python modules and packages

Related Article

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.