Python module knowledge 1: module knowledge introduction, python knowledge Introduction
Modules are code classificationCan define functions, classes, and variables, and allocate relevant code to a module, which makes your code easier to use and easier to understand and simplify. The module is called a class library in java.
How the module exists:
The module can be a single. py file or a single file (which stores n. py files ).
1. Module classification:
Built-in modules:For example, OS and sys are two very common modules that interact with the operating system. file is a module related to file operations. Commonly used modules include logging, time/datetime, json/pickle.
Custom module:Your own py file or folder (which can contain multiple py files)
Third-party module:Such as requests and math
2. Use of modules: the principle is to be used after pilot Import
It is best to put the module and the execution file in the same directory. The import method can use import or from.
The main syntax format is as follows:
Import s1
S1.login () # Use the login function in s1
Import lib. s1
Lib. s1.login () # usage
From s1 import login
Login ()
From s1 import *
Login ()
Logout ()
From lib import s1 as lib_s1
From scr import s1 as scr_s1
3. Module priority rules:
Python first searches for the directory where the project is located. If the directory does not exist, it searches for other default directories in python. The priority is from top to bottom. For specific priorities, see sys. path.
If the file is not in the system search directory, you can use append to add it.
Case 1: The module finds the directory priority.
import sys
for item in sys.path:
print(item)
Search priority:
Case 2: If s2 is not in the system directory, you can add it first and then import the s2 file:
Import sys
Sys. path. append ('d: \ ') # import the D folder
Import s2 # file name
4. Third-party module Import
1) pip3 install module. Python3 comes with pip3. You need to add the directory of pip3 to the environment variable, and then you can directly import the module.
2) pip3 uninstall + uninstall the module
Import module requests
Case:
Step 1: Enter the Directory
Start-cmd. Open the command edit box and enter the following command (cd + space + pip3 directory) To Go To The pip3 directory:
Input command:CdC: \ Users \... \ AppData \ Local \ Programs \ Python \ Python36 \ Scripts
Step 2: Install wheel
Input: pip3 install requests
5. Special variables in the module:
1) view all built-in Variables
Execution result:
2) Comment Information _ doc __
Import s2
Print (_ doc _) # obtain the annotation information and obtain the content in the py program document ""
3) obtain the path _ file _ of the current program __
# Print (_ file _) # obtain the path of the current program
Import OS
Print (OS. path. abspath (_ file _) # C: \ Users \... \ PycharmProjects \ study2017 \ s2.py
Print (OS. path. dirname (OS. path. abspath (_ file _) # C: \ Users \... \ PycharmProjects \ study2017
Print (OS. path. dirname (OS. path. dirname (OS. path. abspath (_ file _) # C: \ Users \... \ PycharmProjects
4) print (_ package _) # obtain the package of the programThe file import contains the package. If your package is imported, None is displayed.
5)Main File Execution _ name __= = "_ main __"
# It is executed only when the current file is executed. The special Variable _ name __= = "_ main _" in the current file. If it is imported, it will not be executed. Therefore, you can write it in the main file, which can be restricted, it is executed only in the main program and not when it is imported as a module
Def run (): # define the run Function
Print ("run ")
Run ()
If _ name __= = "_ main _": # Run only in the main program
Run ()
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.