Python Modules and Packages

Source: Internet
Author: User
Tags define function naming convention

---restore content starts---

A Module introduction

1. What is a module?

#常见的场景: A module is a python file that contains a set of functions, such as spam.py, which is named spam and can be used via import spam. #在python中, the module is used the same way, but in fact, the module can be divided into four general categories: 1. py file 2, written in Python, has been compiled into a shared library or DLL's C or C + + extension 3 folders that organize a series of modules together (note: There is a __ in the folder init__.py file, which is called a package) 4 built-in modules written with C and linked to the Python interpreter

2. Why should I use the module?

#1, organize programs from file level, more convenient management with the development of the program, more and more functions, in order to facilitate the management, we usually divide the program into a file, so that the structure of the program is clearer and easier to manage. At this time, we can not only use these files as scripts to execute, but also as a module to import into other modules, the implementation of the function of the re-use of the same principle, take doctrine, improve the development efficiency, we can also download others to write the module and then import into their own projects, this take doctrine, Can greatly improve our development efficiency #ps: If you exit the Python interpreter and then re-enter, then the functions or variables that you defined previously will be lost, so we usually write the program to a file so that it can be saved permanently and, if necessary, through Python test.py way to execute, at this time test.py is called script scripts.

3, take spam.py as an example to introduce the use of the module: file name spam.py, module name spam

Import of two use modules

1, the use of import

#模块可以包含可执行的语句和函数的定义, the purpose of these statements is to initialize the module, which executes only when the module name is first encountered when importing the import statement (the import statement can be used anywhere in the program and is imported multiple times for the same module. To prevent you from repeating the import, the Python optimization means that the module name is loaded into memory after the first import, and the subsequent import statement only adds a reference to the module object that has been loaded into memory and does not re-execute the statements within the module, as #test. Pyimport Spam #只在第一次导入时才执行spam. py inside the code, the explicit effect here is to print only once ' from the spam.py ', of course, the other top-level code is also executed, but does not show the effect. Import spamimport spamimport spam ' ' Execution result: from the spam.py '

PS: We can find the module that is currently loaded from Sys.module, Sys.module is a dictionary that contains the mapping of the module name to the module object, which determines whether the import module needs to be re-imported.

2, when the first import module will do three things, duplicate import will directly refer to the memory of the loaded results

#1. Create a new namespace for the source file (spam module), and the functions and methods defined in spam are the namespaces that are accessed when using global. #2. Execute the code contained in the module in the newly created namespace, see initial import spam    hint: What exactly did you do when you imported the module? In    fact function definitions is also ' statements ' that is     ' executed '; the execution of a module-level function Definition     enters the function name in the module ' s global symbol table.    In fact, the function definition is also the "executed" statement, the execution of the module-level function definition puts the function name    into the module global namespace table, with Globals () to view the # #. Create a name spam to reference the namespace    the name and the variable name are no different. The first type of ', and the use of spam. Name can be accessed by the name defined in the    spam.py file, spam. Name and test.py name from    two completely different places.

3, the imported module has a separate name space

Each module is a separate namespace, defined in this module of the function, the namespace of the module as the global namespace, so that when we write our own module, we do not have to worry about our definition in our own module global variables will be imported, and the user's global variables conflict

Test One: Money does not conflict with Spam.moneyTest Two: Read1 and Spam.read1 do not conflictTest Three: Global variables to perform the Spam.change () operation money is still in spam

4. Alias for module name

How to alias a module that has already been imported is useful for writing extensible code

1 import spam as sm2 print (Sm.money)

There are two SQL modules MySQL and Oracle, depending on the user's input, choose different SQL functions

Assuming there are two modules xmlreader.py and csvreader.py, they all define function read_data (filename): Used to read some data from a file, but in a different input format. You can write code to selectively pick the read

5. Import multiple modules in one line

1 Import Sys,os,re
three use of the module from ... import :

1. From...import ... The use

1 from spam import read1,read2

2. Comparison of From...import and import

#唯一的区别就是: Use From...import ... is to import the name in the spam directly into the current namespace, so in the current namespace, the name can be used directly, without prefixing: spam. #from ... import ... In a way that's good and bad: it's easy    to use: It    's easy to clash with names in the current execution file

Verify one: The current location is used directly with Read1 and Read2, when executed, still with the spam.py file global namespace

Verification Two: If there is currently duplicate name Read1 or read2, then there will be coverage effect.

Validation three: The imported method is always based on the source file when it is executed

3, also support as

1 from spam import read1 as read

4, a row to import multiple names

From spam import Read1,read2,money

5, From...import *

#from Spam Import * Imports all names in spam that are not preceded by an underscore (_) into the current position # Most of the cases our Python program should not use this type of import, because * you don't know what name you're importing, it's likely to overwrite the name you've already defined. And the readability is extremely poor, there is no problem when importing in an interactive environment.

You can use __all__ to control * (to release the new version) and add a new line to the spam.py

__all__=[' money ', ' read1 '] #这样在另外一个文件中用from spam import * This can import two names specified in the list
Four overloading of modules (learn)

For performance reasons, each module is imported only once, put into the dictionary sys.module, if you change the contents of the module, you must restart the program, Python does not support reloading or uninstalling the previously imported modules,

Some students may think of removing a module directly from the Sys.module can not be uninstalled, note that you deleted the Sys.module module object may still be referenced by other program components, so it will not be clear.

In particular, we refer to a class in this module, which produces many objects with this class, so that these objects have references to this module.

If it's just a module that you want to test interactively, use importlib.reload (), e.g. import importlib; Importlib.reload (ModuleName), which can only be used for test environments.

In 20 seconds of waiting time, modify the contents of func1 in aa.py, waiting for the result of test.py.

Open importlib comments, re-test

five py file distinguishes between two purposes: module and script
#编写好的一个python文件可以有两种用途:    One: script, a file is the whole program, used to be executed    two: module, the file contains a bunch of functions, used to be imported using #python for our built-in global variable __name__,    When a file is executed as a script: __name__ equals ' __main__ ' when the    file is imported as a module: __name__ equals the module name # function: Used to control the. py file in different scenarios to perform different logic    if __name__ = = ' _ _main__ ':
Six module search path

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

Website Explanation:

#官网链接: Https://docs.python.org/3/tutorial/modules.html#the-module-search-path Search Path: When a module named spam is imported    The interpreter first looks for the name from the built-in module    , and then goes to Sys.path to find the name Sys.path the    1 execution file is initialized from the following location in the current directory    2 Ptyhonpath (contains a list of directory names, As with the shell variable Path syntax)    3 relies on the default designation when installing: In a file system that supports soft connections, the directory in which the script is executed is computed after a soft connection, in other words, the directory containing the soft connection is not added to the module's search path after initialization, We can also modify the Sys.path in the Python program, the path where the executable file is located is the first directory of Sys.path, in front of all standard library paths. This means that the current directory takes precedence over the standard library directory, and it needs to be emphasized that our custom module names are not duplicated with the module name of the Python standard library, unless you are deliberately, silly.
Seven compiling Python files (learn)

To increase the speed of loading modules, emphasis is placed on emphasizing that loading speed is not the speed of 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. For example, under the CPython3.3 version, the spam.py module is cached as __PYCACHE__/SPAM.CPYTHON-33.PYC. This naming convention guarantees a multi-version coexistence of compiled results.

Python checks that the modification time of the source file is compared to the compiled version and needs to be recompiled if it expires. This is a completely automated process. And the compiled module is platform independent, so the same library can be shared between different architectures of the system, that is, PYc makes a cross-platform bytecode, similar to Java fire. NET, is executed by Python virtual machine, but the content of PYC is related to Python version, Different versions of the compiled PYC file, 2.5 compiled PYc file can not be executed to 3.5, and PYc file is able to decompile, so it is only used to increase the loading speed of the module, not for encryption.

details ofEight Package Introduction

1. What is a package?

#官网解释Packages is a way of structuring Python's module namespace by using "dotted module names" package is an organization of Python module names by using '. Module name ' Space in a way. #具体的: The package is a folder containing __init__.py files, so in fact we create the package is to use folders to organize files/modules # need to emphasize is: 1. In the 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 package 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 essence of the package is a module

2, why to use the package

The essence of a package is a folder, then the only function of the folder is to organize the file as more and more features, we can not put all the functions in a file, so we use the module to organize functions, and as the module more and more, we need to use a folder to organize the module files, To improve the structure and maintainability of the program.

3. Precautions

#1. Import statements about package related are also divided into import 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. But for import, there is no such restriction when it is used, and the left side of the point can be a package, a module, a function, a class (both of which can call their own properties in a point-and-click manner). When importing a file #2, import, the name of the generated namespace 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 # #, package A and package B under the same name module will not conflict, such as A.A and B.A from two namespaces

4. Class Flow

Use of nine packs

1. Model Documents

glance/                   #Top-level package├──__init__.py      #Initialize The Glance package├──api                  #Subpackage for api│   ├──__init__.py│   ├──policy.py│   └──versions.py├──cmd                #Subpackage for cmd│   ├──__init__.py│   └── Manage.py└──db                  #Subpackage for db    ├──__init__.py    └──models.py

Execute file and model file in sibling directory

2. Import of the use of the package

1 Import glance.db.models2 glance.db.models.register_models (' MySQL ')

Importing package names individually does not import all of the contained sub-modules in the package, such as

#在与glance同级的test. PY Import glanceglance.cmd.manage.main () ' Execution result: attributeerror:module ' glance ' has no attribute ' CMD ""

Workaround:

1 #glance/__init__.py2 from. Import cmd3 4 #glance/cmd/__init__.py5 from. Import Manage

Perform:

1 #在于glance同级的test. py 2 Import glance3 glance.cmd.manage.main ()

3, the use of the package from ... import ...

It is important to note that the From Post import module must be clear one cannot take a point, otherwise there will be a syntax error, such as: From a import B.C is the error syntax

1 from glance.db import models2 models.register_models (' MySQL ') 3 4 from glance.db.models import REGISTER_MODELS5 Register _models (' MySQL ')

4. 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:

1 #在__init__. PY Definition 2 x=103 4 def func (): 5     print (' from api.__init.py ') 6 7 __all__=[' x ', ' func ', ' policy ']

At this point we are in the glance of the files in the same class to execute from GLANCE.API import * To import the contents of the __all__ (versions still cannot import).

Practice:

#执行文件中的使用效果如下, please handle the import of the package from Glance Import *get () create_resource (' a.conf ') Main () register_models (' MySQL ')
View Code

5. Absolute import and relative import

Our top package glance is for others to use, and then within the glance package will also have each other to import each other needs, this time there are absolute import and relative import two ways:

Absolute import: Starting with glance

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

For example: We want to import glance/cmd/manage.py in glance/api/version.py

1 in Glance/api/version.py2 3 #绝对导入4 from glance.cmd import Manage5 manage.main () 6 7 #相对导入8 from: CMD import manage9 manage.main ()

Test Result: note must be tested in glance peer files

1 from GLANCE.API import versions

6, the package and the modules contained in the package are used to be imported, but not directly executed. And the environment variables are based on the execution file.

For example, we want to import glance/api/policy.py in the glance/api/versions.py, some students a smoke these two modules are in the same directory, very happy to do, it directly to do

1 #在version. PY 2 3 Import policy4 Policy.get ()

Yes, we run version.py alone is a bit of a problem, the path to run the version.py search is starting from the current path, so when importing policy can be found in the current directory

But you want to ah, the module in your sub-package version.py is likely to be imported by a glance package of the same level of other files, such as we are in glance peer under a test.py file import version.py, as follows

1 from GLANCE.API import versions 2  3 "4 execution Result: 5 importerror:no module named ' Policy ' 6" ' 7  8 "' 9 Analysis: 10 at this time We import versions in versions.py to execute one import policy need to find from Sys.path that is, from the current directory to find policy.py,12 this must not be found in the 13 "

Ten Software development Specifications

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.