Introduction to python module

What is a module

Module is equivalent to the toolkit in python, which packages the code of specific functions into modules and calls them when needed

Module benefits

  • Lightweight program: the module can simplify the software and speed up the reading speed. It can be called when needed
  • Convenient reference: package the code of specific functions into modules, which can be quickly accessed when needed
  • Convenient maintenance: as long as the module is maintained / updated, the maintenance / update of all called modules can be realized
  • Avoid conflicts between function and variable names: each module has an independent namespace, which can avoid conflicts with functions or variables with the same name in other modules

Module classification

  • Built in standard library
  • Third party module: the program is written and shared by apes themselves
  • Custom module: written by yourself

Import & call

import module name (multiple modules can be imported at the same time, separated by commas)

>>> import os,sys
>>> os.getcwd()
'/Users/apple/Documents'

Sometimes the whole module is too large. You only need to use the specific method under the module. You can only import this method, for example, only import the getcwd method under the os module.
Note that when using this method to import, you do not need to use OS Getcwd(), instead of using getcwd()

>>> import os,sys
>>> from os import getcwd
>>> getcwd()
'/Users/apple/Documents'

It also supports importing multiple files at the same time

>>> from os import getcwd,path
>>> path.getctime()

You can even import a specific function in the PY file in the module (you can view all methods / py files under the current module in pycharm)

>>> from os.path import getctime
>>> getctime('/Users/apple/Desktop/test.txt')
1597738069.403486

When importing a module, you can use as to rename the module, which is usually used to simplify the name

>>> from os.path import getctime as ctime
>>> ctime('/Users/apple/Desktop/test.txt')
1597738069.403486

All methods under the import module. Note: not recommended. Because after importing in this way, you can directly use name() as follows. If there is a name method in both modules, calling name() at this time may lead to unforeseen errors and even conflict with the defined name variable

>>> from os import * #Use with caution!!!
>>> from sys import * #Use with caution!!!
>>> name()

Module reference specification

1 . Module import is written at the beginning of the program for easy reading. Do not import modules in the middle of the code, which is very poor readability
2. Module import code priority order: built-in module > third-party module > self written module

Module path

By default, modules written by yourself can be imported only in the same level file directory, but not in another path. This is related to the module path set by python.

>>> import sys
>>> sys.path
[
'', 
'/Users/apple/Documents', 
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip', 
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8', 
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload', 
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages'
]
  • When importing a module, python will automatically find the name of the module you want to import in turn in the path list. As long as it matches, it will be imported without looking back
  • The first null value in the list indicates the directory of the current execution file. In addition, lib is the built-in standard module folder, and site packages is the third-party module. Usually, we will put the custom module into the third-party module, so that we can call it at any time
  • You can use sys Edit the list of path to add the path of module call
import sys
sys.path.append("Path to add")

Third party module installation and pip source change

Open the official community and search the module name you want,
Use the pip command to install

pip3 install Module name

Generally, it will be automatically installed into the site packages folder

The pip command will be downloaded to the official server abroad by default, which is relatively slow. It is recommended to use the domestic Douban source. The data will be synchronized with the foreign official website regularly, which is much faster

pip3 install -i Domestic website name --trust-host Website name
  • -i is followed by the source address
  • – trust host should be added, which is the security mechanism of python, followed by the website name

As you know, the link speed of the default foreign source is sometimes very slow. There are many mirror websites in China, which will synchronize the foreign resources in time. We can quickly download the module by changing the source

  • Watercress http://pypi.douban.com/simple/
  • tsinghua
    https://pypi.tuna.tsinghua.edu.cn/simple

The command is as follows:

pip3 install -i http://pypi.douban.com/simple --trusted-host pypi.douban.com packagename # packagename is the name of the package to be downloaded
pip3 install -i http://e.pypi.python.org --trusted-host e.pypi.python.org --upgrade pip # upgrade pip

Mechanism of module import

  • import os:
    When importing a module, a module namespace and a global namespace (the program space of the calling module) will be created. When OS. Net is used in the global space When getcwd(), the OS of the global space Getcwd () refers to the getcmd() function of the module space
  • from os import getcwd:
    When importing a module, the previous steps are the same as above, except that getcwd() in the global space points to the function memory address of getcwd() in the module, rather than the function name (variable) of the function memory address in the module

Circular import

  • Two files are imported into each other as module calls
  • This operation must be avoided as far as possible
  • However, if it does happen and the cost of modification is very high, the emergency method is to put the import module statement into the function, do not let the call occur automatically, but call through the function

Keywords: Python

Added by Darkmatter5 on Fri, 18 Feb 2022 10:37:09 +0200