Import usage of Python3 modules and packages

One, module import

1. Definition

A Python module (Module) is a Python file that ends in .py and contains Python object definitions and Python statements.

Modules allow you to logically organize your Python code sections.

Assigning related code to a module can make your code more usable and easier to understand. 

Modules can define functions, classes and variables, and modules can also contain executable code.

Including: built-in modules, custom modules, third-party modules;

2. Function

The biggest advantage is that the maintainability of the code is greatly improved. Secondly, writing code does not have to start from scratch. When a module is written, it can be referenced elsewhere. When we write programs, we often refer to other modules, including built-in Python modules and modules from third parties.

Using modules can also avoid conflicts between function names and variable names. Functions and variables with the same name can be stored in different modules. Therefore, when we write the module ourselves, we don't have to consider that the name will conflict with other modules. But also pay attention, try not to conflict with the names of built-in functions.

 

3.import

After the module is defined, we can use the import statement to import the module, the syntax is as follows:

import module1[, module2[,... moduleN]

Example:

Copy code

#spam.py
print('from the spam.py')

money=1000

def read1():
    print('spam->read1->money',money)

def read2():
    print('spam->read2 calling read')
    read1()

def change():
    global money
    money=0

Copy code

Import module

import spam #Execute the code in spam.py only when it is imported for the first time. The explicit effect here is to print'from the spam.py' only once. Of course, other top-level codes are also executed, but there is no display effect.
Execution result: 
 from the spam.py

Note: Modules can contain executable statements and function definitions. The purpose of these statements is to initialize the module. They are only executed when the module name encounters the import statement for the first time (import statements can be used anywhere in the program Yes, and the same module is imported many times. In order to prevent you from re-importing, the optimization method of python is: the module name is loaded into the memory after the first import, and the subsequent import statement is only for the large memory that has been loaded Add a reference to the module object, and the statement in the module will not be re-executed)

 

Import import modules do:
1. Generate a new name space
2. Use the newly created name space as the global name space and execute the code of the file
3. Get a module name spam, pointing to the name space generated by spam.py

 

  • Test 1: money does not conflict with spam.money

Copy code

#Test one: money and spam.money do not conflict 
#test.py 
import spam 
money=10 
print(spam.money) 

''' 
Execution result: 
from the spam.py 
1000 
'''

Copy code

 

 

  • Test 2: read1 does not conflict with spam.read1

Copy code

#Test two: read1 and spam.read1 do not conflict 
#test.py 
import spam 
def read1(): 
    print('========') 
spam.read1() 

''' 
Execution result: 
from the spam. py 
spam->read1->money 1000 
'''

Copy code

 

  • Test 3: The global variable money that executes the spam.change() operation is still the money in spam

Copy code

#Test three: The global variable money that executes the spam.change() operation is still 
#test.py 
import spam 
money=1 
spam.change() 
print(money) 

''' 
Execution result: 
from the spam.py 
1 
'''

Copy code

 

  • as, alias the module name
import spam as sm #sm is the alias of spam 
 print(sm.money)

Example:

The way to alias an imported module is very useful for writing scalable code. Suppose there are two modules, xmlreader.py and csvreader.py, both of which define the function read_data(filename): used to read some data from a file , But uses a different input format. You can write code to selectively select the reading module, for example

if file_format == 'xml':
     import xmlreader as reader
elif file_format == 'csv':
    import csvreader as reader
data=reader.read_date(filename)

 

  • Import multiple modules in one line
import sys,os,re

 

4.From…import

from modname import name1[, name2[, ... nameN]]

To import the fibonacci function of the module fib, use the following statement:

from fib import fibonacci

This statement will not import the entire fib module into the current namespace, it will only introduce the fibonacci in fib into the global symbol table of the module that executes this statement.

 

from... import.. Import modules:

1. Generate a new name space
2. Use the newly created name space as the global name space and execute the code of the file
3. Get directly the name in the name space generated by spam.py

 

Copy code

#Test 1: The imported function read1, when executed, it still returns to the spam.py to find the global variable money 
#test.py 
from spam import read1 
money=1000 
read1() 
''' 
Execution result: 
from the spam.py 
spam-> read1->money 1000 
''' 

#Test two: The imported function read2 needs to call read1() during execution, and still return to spam.py to find read1() 
#test.py 
from spam import read2 
def read1(): 
    print ('==========') 
read2() 

''' 
Execution result: 
from the spam.py 
spam->read2 calling read 
spam->read1->money 1000 
'''

Copy code

 

But if there is currently the same name read1 or read2, it will have an overwrite effect.

Copy code

#Test 3: The imported function read1 is covered by the read1 defined at the current position 
#test.py 
from spam import read1 
def read1(): 
    print('=========') 
read1() 
' '' 
Execution result: 
from the spam.py 
========== 
'''

Copy code

One point that needs to be particularly emphasized is: variable assignment in python is not a storage operation, but just a binding relationship, as follows:

Copy code

from spam import money,read1 
money=100 #Binding the name money of the current location to 100 
print(money) 
#Print the current name read1() #Read the name money in spam.py, still 1000 

''' 
from the spam.py 
100 
spam->read1->money 1000 
'''

Copy code

 

from ... import ...
Advantages: Convenient, no need to add prefix
Disadvantages: easy to conflict with the name space of the current file

 

  • from ... import... also supports as and importing multiple modules
from spam import read1 as read

from spam import (read1,
                  read2,
                  money)

 

  • from spam import *

Import all names in spam that do not start with an underscore (_) to the current location. In most cases, our python program should not use this import method, because *you don’t know what name you import, it is likely to be Overwrite the name you have previously defined. And the readability is extremely poor, there is no problem when importing in an interactive environment. 

Copy code

from spam import * 
#Import all the names in the module spam into the current namespace 
print(money) print(read1) 
print(read2) 
print(change) 

''' 
Execution result: 
from the spam.py 
1000 
<function read1 at 0x1012e8158> 
<function read2 at 0x1012e81e0> 
<function change at 0x1012e8268> 
'''

Copy code

 

  • __all__ to control* (used to release new versions)

Add a new line in spam.py

__all__=['money','read1'] #In this way, use from spam import * in another file to import the two names specified in the list

 

  • __name__

spam.py is executed as a script, __name__='__main__'
spam.py is imported as a module, __name __=module name

Function: used to control the .py file to execute different logic in different application scenarios

Copy code

#fib.py

def fib(n):    # write Fibonacci series up to n
    a, b = 0, 1
    while b < n:
        print(b, end=' ')
        a, b = b, a+b
    print()

def fib2(n):   # return Fibonacci series up to n
    result = []
    a, b = 0, 1
    while b < n:
        result.append(b)
        a, b = b, a+b
    return result

if __name__ == "__main__":
    import sys
    fib(int(sys.argv[1]))

Copy code

carried out

#python fib.py <arguments> 
python fib.py 50 #On the command line

 

5. The search path of the module

The search order of modules is: modules already loaded in memory -> built-in modules -> modules included in the sys.path path

The python interpreter will automatically load some modules when it starts. You can use sys.modules to view. When importing a module for the first time (such as spam), it will first check whether the module has been loaded into the memory (the current execution file The memory corresponding to the name space), if there is a direct reference

If not, the interpreter will look for the built-in module with the same name, and if it has not been found, it will look for the spam.py file in turn from the directory list given by sys.path.

Pay special attention to: The name of the self-defined module should not be the same as the name of the system built-in module.

 

After initialization, the python program can modify sys.path, and the path that is put in front has priority over the standard library to be loaded.

 

Note: When searching, search in the order from left to right in sys.path. The first one will be searched first. sys.path may also contain .zip archive files and .egg files. Python will treat .zip archive files as a directory. To deal with,

Copy code

#First make the archive file: zip module.zip foo.py bar.py 

import sys 
sys.path.append('module.zip') 
import foo,bar #You 

can also use the specific location of the directory structure in the zip 
sys.path.append ('module.zip/lib/python')

Copy code

 

Note: If the path under windows does not start with r, it will have a syntax error

The path under windows does not start with r, it will have a syntax error 
sys.path.insert(0,r'C:\Users\Administrator\PycharmProjects\a')

 

Second, the import of the package

1. Definition

Package is a way to organize python module namespace by using'.module name'.

Regardless of the import form or the from...import form, whenever you encounter a dot in the import statement (not when you use it), you must be alert for the first time: this is the import syntax that is unique to the package. The left side of. Must be a bag;

A package is a hierarchical file directory structure, which defines a Python application environment composed of modules, sub-packages, and sub-packages under sub-packages. Simply put, a package is a folder, but the __init__.py file must exist in the folder, and the content of the file can be empty. __int__.py is used to identify the current folder is a package.

A package is a collection of modules. A module is a collection of functions and classes that deal with a certain type of problem.

Example:

Copy code

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

Copy code

document content

Copy code

#文件内容

#policy.py
def get():
    print('from policy.py')

#versions.py
def create_resource(conf):
    print('from version.py: ',conf)

#manage.py
def main():
    print('from manage.py')

#models.py
def register_models(engine):
    print('from models.py: ',engine)

Copy code

 

2.import

Before using a function or class in a module, you must first import the module. The import of the module uses the import statement.

When calling a function or class of a module, the module name must be used as a prefix.

When importing a file, the name in the generated namespace comes from the file, and when importing the package, the name of the generated namespace also comes from the file, that is, __init__.py under the package. Importing the package is essentially importing the file

Example 
Test in a file at the same level as the package glance
import glance.db.models
glance.db.models.register_models('mysql') 

Anything with a dot during import must be a package to the left of the dot.

 

3.from ... import ...

If you don't want to use the prefix in the program, you can use the from...import... statement to import the module.

It should be noted that the module imported by import after from must be a clear one without dots, otherwise there will be grammatical errors, such as: from a import bc is the wrong grammar

Example

Test in a file at the same level as the package glance 

from glance.db import models
models.register_models('mysql')

from glance.db.models import register_models
register_models('mysql')

 

The current path of the executed file is sys.path

import sys
print(sys.path)

 

note:

1. The import statements related to the package are also divided into import and from...import..., but no matter which type, no matter where it is, a principle must be followed when importing: everything with dots when importing , The left side of the dot must be a packet, otherwise it is illegal. It can have a series of points, such as item.subitem.subsubitem, but they must follow this principle.

2. After importing, there is no such restriction when in use. The left side of the dot can be packages, modules, functions, and classes (all of them can call their own properties in a dot manner).

3. Compare the application scenarios of import item and from item import name:
if we want to use name directly, we must use the latter.

 

4.__init__.py file

Either way, as long as the package is imported for the first time or any other part of the package, the __init__.py file under the package will be executed in turn (we can print a line of content in each package file to verify it) , This file can be empty, but it can also store some initialization package code.

 

5.from glance.api import *

Import everything from the package api. In fact, this statement will only import the name defined in the __init__.py file under the package api. We can define __all___ in this file:

Copy code


    #Define 
x=10 in __init__.py 

def func(): print('from api.__init.py') 

__all__=['x','func','policy']

Copy code

At this point, we execute from glance.api import * in the file at the same level of glance and import the content in __all__ (versions still cannot be imported).

 

6. Absolute import and relative import

The top-level package glance is written for others to use, and there will also be mutual import requirements within the glance package. At this time, there are two ways of absolute import and relative import:

Absolute import: start with glance

Relative import: use. Or .. as the starting point (can only be used in one package, not in different directories)

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

Copy code

In Glance / API / version.py 

# absolute introduced 
from glance.cmd Import Manage 
manage.main () 

# opposing introduced 
from ..cmd Import Manage 
manage.main ()

Copy code

Test result: Note that it must be tested in a file at the same level as glance

from glance.api import versions 

 

Note: You can use import to import built-in or third-party modules (already in sys.path), but absolutely avoid using import to import submodules of custom packages (not in sys.path), you should use from... import The absolute or relative import of ..., and the relative import of packages can only be in the form of from.

 

7. Import the package separately

When the package name is imported separately, all the submodules contained in the package will not be imported, such as

Copy code

# Glance at the same level in test.py 
Import glance 
glance.cmd.manage.main () 

'' ' 
executed Results: 
AttributeError: Module1' glance 'attribute has NO' cmd ' 

' ''

Copy code

Solution:

#glance/__init__.py
from . import cmd
 
#glance/cmd/__init__.py
from . import manage

carried out:


#Is in the test.py at the same level of 
glance import glance glance.cmd.manage.main()

Don’t ask: __all__ can’t be solved, __all__ is used to control from...import *

 

Guess you like

Origin blog.csdn.net/xcntime/article/details/114969141
Recommended