python base (13): and a packet module

I. Introduction module

        The actual development is impossible without a standard module to the system, or third-party modules.

        If you want to achieve time-related features, you need to call the system time module. If you want to achieve the files and folders related to the operation, you need to use the os module. As another example of our Web automated testing done by Selenium, Selenium for python so it is a third-party extension module.

        Each python can be used as a module. Module in the form of a disk file. When a module becomes too large, and too much driving force function, then you should consider dismantling some code out of another to build a module. Modules in the period of script code can be executed directly, it can be a bunch of similar library functions in code, which can be imported into other modules (import) call. Module may comprise a block of code that runs directly, class definition, or a combination of these function definition persons.

        Recommended for all beginning to import module python modules. Further preferably in this order:

  • python standard library modules
  • python third-party modules
  • Custom application modules

Import ways:

        In python import keyword used to introduce a module, for example, to import the module time, it can import time to introduce in place by the beginning of the file.

import module1
import module2[
……
import moduleN]

or

import module1[, module2, ……, moduleN]

Call the method:

模块名.函数名

Example:

import time

print('start')
time.sleep(5)
print('stop')
# output:输出start后隔5秒再输出stop

from ... import ...

        Introducing a specified portion from the module into the current namespace.

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

from ... import *

        Import all of the contents of a module.

from modname import *

from ... as ...

        Sometimes you import the module name is already used in your program, or if you do not want to use an existing name. You can replace the original name with a new name.

Example:

import pandas as pd

Second, the module production

        For a software project, it can not put all your code in one file to achieve, they are usually implemented in different directories and files according to certain rules.

Production modules:

        Very simple, i.e. .py module file, which contains the functions, etc., the module name is the file name .py.

Calling module to produce their own:

When you import a module, python parser search order for the location of the module are:

  1. Current directory
  2. No current directory, then search each directory in the environment variable PYTHONPATH
  3. If you can not find, python will see the default path. In UNIX, the default path is typically / usr / local / lib / python /
  4. Module search path is stored in the variable sys.path system module. Variable contains the current directory, PYTHONPATH and the default directory is determined by the installation process.

When introduced into the sub-modules in the same directory:

import 同级文件夹名.文件夹下模块名

When introducing different level directories:

import sys
sys.path.append('..\\')  # 返回上一级目录
import modname

        Therefore, if the standard module of the same module or PYTHONPATH current path exists, the standard module is overwritten. That is, if there is xml.py current directory, then in the implementation of import xml, importing a module in the current directory, rather than the standard system xml.py.

Three, dir () function with the standard module

to you():

        dir used by search module definition module name, which returns a stored list of character string types. Which lists all types of names: variables, modules, functions, and so on.

Example:

import time
print(dir(time))

输出:['_STRUCT_TM_ITEMS', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'altzone', 'asctime', 'clock', 'ctime', 'daylight', 'get_clock_info', 'gmtime', 'localtime', 'mktime', 'monotonic', 'monotonic_ns', 'perf_counter', 'perf_counter_ns', 'process_time', 'process_time_ns', 'sleep', 'strftime', 'strptime', 'struct_time', 'thread_time', 'thread_time_ns', 'time', 'time_ns', 'timezone', 'tzname']

Fourth, the standard module

        Some modules are built directly in the parser, although some of these languages ​​are not built-in features, but was able to very efficiently use, and even system-level calls, no problem. These components will be different forms depending on the configuration of the system to do early, such as winreg (windows registry access) This module will only be provided to the windows system. It should be noted that there is a special module sys, which is placed in each python parser.

V. package

  • Python package is a form of management module namespace used. "Module name." Such as the name of a module is AB, then it represents a sub-module packet A in B.
  • Only directory contains a file named __init__.py will be considered as a package.
  • When importing the package, python will be to find this package contains subdirectories from sys.path directories.

Import packages:

        Some link the two existing modules function, so place it under the same folder, a file class inherits from another class file.

  • . Papers introduction import module
import modname
  • Import from a folder module according introduced
from fname import *
# 导入文件夹内所有模块

Production package:

      When you create __init__.py, pay attention:

  • Only directory contains a file called __init__.py, in order to be recognized as a program package, the module can be imported successfully.
  • __init__.py file to be written line: __ all__ = [allows modules to be introduced]
__all__ = ["modname1", "modname2"]
  • You can write other content in __init__.py, the import content of these written will be executed.
  • __Init__.py can be added to sys.path in the current module is called path.
  • Write module is not recommended in __init__.py, try to ensure that this file is simple.

imp.reload():

 

Example:

import test
# import test   # 引入两边只输出一句hahaha
import imp
imp.reload(test)  # 此时可以输出两边hahaha

 

Guess you like

Origin blog.csdn.net/qq_26271435/article/details/89847035