Detailed explanation of Python's custom modules


foreword

  In Python, a custom module has two functions: one is to standardize the code, making the code easier to read, and the other is to facilitate other programs to use the code that has been written to improve development efficiency.
  Implementing a custom module is mainly divided into two parts, one is to create a module , and the other is to import a module .


1. Create a module

  When creating a module, you can write the relevant code in the module (variable definition and function definition, etc.) in a separate file, and name the file in the form of "module name + .py " .

注意:创建模块时,设置的模块名不能是Python自带的标准模块名称(比如time、math、os等等)。

  For example, create a module dedicated to downloading pictures and name it download_picture.py, where download_picture is the module name and .py is the extension. Part of the code is as follows:

    def requests_image_url(self, image_url, image_title):
        '''
            5.对图片地址发起请求
        '''
        response_second = self.session.get(image_url).content
        self.create_dir(response_second, image_title)

    def create_dir(self, response_second, image_title):
        '''
            6.创建文件夹
        '''
        if not os.path.exists(self.keyword):
            os.mkdir(self.keyword)
        self.save_images(response_second, image_title)

    def save_images(self, response_second, image_title):
        '''
            7.保存图片
        '''
        try:
            with open('{}/{}.jpg'.format(self.keyword, image_title), 'wb') as f:
                f.write(response_second)
            number = len(os.listdir(r'C:\Users\administrator0\Desktop\666\全网图片抓取\{}'.format(self.keyword)))
            print('第{}张图片{}下载成功'.format(number, image_title))
        except Exception as e:
            print('图片{}下载成功'.format(image_title))

注意:模块文件的扩展名必须是 “.py”


2. Use the import statement to import modules

  Once a module is created, it can be used in other programs. To use a module, you need to load the code in the module as a module, which can be achieved using the import statement. The basic syntax of the import statement is as follows:

import modulename [as alias]

  where modulename isThe name of the module to import;[as alias] isan alias for the module, the module is also available through this alias. The following will import the module download_picture.py
  just created to download pictures , and execute the method in this module. Create a file named 1.py in the same directory as the module file download_picture.py. In this file, import the module download_picture and execute the main() function in the QwtpSpider() class in this module. The code is as follows:

import download_picture  # 导入download_picture模块
picture = download_picture.QwtpSpider()  # 调用模块中的QwtpSpider()类
picture.main()  # 执行该类的main()方法

  Execute the above code to display the following results.

insert image description here

Note : When calling a variable, function or class in a module, you need to add " module name. " as a prefix before the variable name, function name or class name. For example, download_picture.QwtpSpider in the above code means calling the QwtpSpider() class in the download_picture module.

If the module name is long and difficult to remember, you can use the as keyword to set an alias for   it when importing the module , and then you can use this alias to call variables, functions, and classes in the module. For example, modify the above code that imports the module to the following.

import download_picture as d  # 导入download_picture模块并设置别名为d

  Then, when calling the QwtpSpider() class in the download_picture module, you can use the following code:

picture = d.QwtpSpider()  # 调用模块中的QwtpSpider()类

  Using the import statement alsoMultiple modules can be imported at once, when importing multiple modules, between the module namesUse a comma "," to separate. For example, three module files, download_picture, download_audio, and download_video, are created respectively. To import all three module files, you can use the following code:

import download_picture, download_audio, download_video

3. Use the from...import statement to import modules

  When using the import statement to import a module, each execution of an import statement will create a new namespace , and execute all statements related to the .py file in this namespace. When executing, you need to add "module name." prefix before the specific variable, function and class name. If you don't want to create a new namespace every time you import a module, but import specific definitions into the current namespace, you can use the from...import statement. After importing a module using the from...import statement,No need to add a prefix, directly access through specific variables, functions and class namesThat's it.

Explanation : A namespace can be understood as a space that records the correspondence between object names and objects. At present, most of Python's namespaces are implemented through dictionaries (dict). Among them, key is an identifier; value is a specific object. For example, key is the name of the variable and value is the value of the variable.

  The syntax format of the from…import syntax is as follows:

from modelname import member

Parameter description :

  • modelname : The module name, which is case-sensitive and needs to be consistent with the case of the module name set when defining the module.
  • member : Used to specify variables, functions, or classes to be imported. Multiple definitions (variables, functions, or classes, etc.) can be imported at the same time, and each definition is separated by a comma ",". If you want to import all definitions, you can also use the wildcard asterisk "*" instead.

  For example, the following two statements can import the specified definition from the module.

from download_picture import QwtpSpider  # 导入模块中的QwtpSpider类
from download_picture import *  # 导入模块中的全部类,变量,函数

4. Module search directory

  When using the import statement to import a module, by default, the module path is searched in the following order.

  1. Search in the current directory (that is, the directory where the executed Python script file is located)
  2. Go to each directory under PYTHONPATH (environment variable) to find
  3. Go to the default installation directory of Python to find

  The specific locations of the above directories are stored in the sys.path attribute of the standard module sys . The sys module is an interface for interacting with the Python interpreter. The specific directory that the module queries can be output through the following code.

import sys
print(sys.path)

  Executing the above code will display the result as shown in the figure below.

insert image description here

  As can be seen from the figure above, when importing any module, it follows the above three path search sequences to find out whether the module exists on the machine. First find the directory where the current Python file is located; if not, then find it from the environment variable directory of Python configuration; if not, find it in the Python installation directory. If there are none, an error will be reported when importing the module as shown in the figure below.

insert image description here

  At this time, we can add the specified directory to sys.path in the following three ways. Before adding it, let's take a look at the specific function code in the custom module download, just print out a sentence, as shown in the figure below.

insert image description here

1. Temporary addition

  Add it temporarily , that is, add it in the Python file that imports the module. For example, if we want to import the download module in the current 1.py file, we first need to find the path of the download.py file "C:\Users\administrator0\Desktop\tool", add the path to sys.path, and then print Output the value of sys.path, the code and the running result are shown in the figure below.

insert image description here

  Then import the download module in the code to run successfully, as shown in the figure below.

insert image description here

Note : The directory added by this method is only valid in the window that executes the current file, and other windows are invalid, so it is called temporary addition. For example, as shown in the figure below.

insert image description here

2. Add .pth file (recommended)

  In the Lib\site-packages subdirectory under the Python installation directory (for example, if the blogger’s Python is installed in the E:\anaconda3 directory, then the path is E:\anaconda3\Lib\site-packages), as shown in the figure below .

insert image description here

Open the directory and create a file with the extension .pth   in it , and the file name is arbitrary. Create an a.pth file here, and add the directory where the module to be imported is located in this file. For example, add the module directory C:\Users\administrator0\Desktop\tool to the a.pth file and save it. Of course, you can also create a new text document first, and write C:\Users\administrator0\Desktop\tool in it, as shown in the figure below.

insert image description here
  After saving, change the file extension to .pth, as shown in the figure below.

insert image description here

注意:创建 .pth 文件后,需要重新打开要执行的导入模块的Python文件,否则新添加的目录不起作用

  Next, the download module can be imported in any file and used normally. As shown below.

insert image description here

Note : The directory added by this method is only in thecurrent versionIt is valid in Python, if other versions of Python are installed and the interpreter is changed to another version, the directory will be invalid.

3. Add to the PYTHONPATH environment variable (recommended)

  Open the "Environment Variables" dialog box (see below for details).
  First, press the Win + R keys at the same time to open the run box, enter sysdm.cpl in it , and click OK , as shown in the figure below.

insert image description here

  Second, click Advanced , and then click Environment Variables

insert image description here

  Check whether there is a PYTHONPATH system environment variable in the dialog box, if not, create one first, the creation method is as follows.
  Click New , as shown in the figure below.

insert image description here

Enter the variable name PYTHONPATH   in the pop-up New System Variable dialog box , and the input variable value is the path of the custom module . The path of the module download I defined is C:\Users\administrator0\Desktop\tool, write it in, and then follow Click OK ,Remember there are three sure to click,As shown below.

insert image description here

注意:在环境变量中添加模块目录后,需要重新打开要执行的导入模块的Python文件,否则新添加的目录不起作用。

  Next, the download module can be imported in any file and used normally. As shown below.

insert image description here

Note : The directory added by this method can be found indifferent versionsShared in Python.

Guess you like

Origin blog.csdn.net/2201_75641637/article/details/129793383