What is a module: py files are modules
What is a package: contains__ init__.py module folder (directory)
The function of modules and packages is to organize code
Note: the directory is not a package. There is no directory in it__ init__.py module
1, Module import
Use functions, variables and classes in other modules to import modules
Function: Use / call the written code
Module import method:
from path route. Module name # import # class name (function name)
import path route. The module name path starts from the project root directory
import package name
import ddt import json import unittest from FutureLoan.common.requests_handler import visit from FutureLoan.middleware.MiddleHandler import MidInitHandler
2, Module classification
Built in module: Python's own module, import os
Third library module: modules written by others need to be installed. pip install requests,import. . . . from····import······
Custom module: a module written by yourself, usually in a package. from package import module
Extended content:
from ... Import * ------ > means to import all code from the module
from FutureLoan.common.requests_handler import * # Don't use it generally. It may have the same name as the function in this module
from .....import sth as other indicates as renaming, alias
from FutureLoan.common.requests_handler import visit as visit_new
Print import module path
import sys print(sys.path) ''' ['D:\\Program Files (x86)\\PyCharm\\workspace\\Lemon', 'D:\\Program Files (x86)\\PyCharm\\workspace', 'D:\\Program Files (x86)\\PyCharm\\workspace\\SmartCampus_web', 'D:\\Program Files (x86)\\Python38\\python38.zip', 'D:\\Program Files (x86)\\Python38\\DLLs', 'D:\\Program Files (x86)\\Python38\\lib', 'D:\\Program Files (x86)\\Python38', 'D:\\Program Files (x86)\\Python38\\lib\\site-packages', 'D:\\Program Files (x86)\\Python38\\lib\\site-packages\\win32', 'D:\\Program Files (x86)\\Python38\\lib\\site-packages\\win32\\lib', 'D:\\Program Files (x86)\\Python38\\lib\\site-packages\\Pythonwin'] '''
III__ name__
Represents the module name of the current file
Run files and scripts {1 Is a module, is a special module 2 We run the program through this file
Run in the form of module import__ name__ File name and module name
Directly running files, scripts__ name__ It is not a file name or module name, but a fixed one:__ main__
demo.py if __name__=='__mian__': print('demo') demo1.py if __name__=='__mian__': print('demo1') demo2.py if __name__=='__mian__': print('demo2') demo3.py from LianXi.test1 import demo from LianXi.test1 import demo1 from LianXi.test1 import demo2 if __name__=='__mian__': print('demo3') #The results after operation are: demo3
demo.py if __name__=='__mian__': print('demo') demo1.py if __name__=='__mian__': print('demo1') demo2.py if __name__=='__mian__': print('demo2') demo3.py from LianXi.test1 import demo from LianXi.test1 import demo1 from LianXi.test1 import demo2 print('demo3') #The results after operation are: demo demo1 demo2 demo3
Question 1: import module Is it OK to write the function like this?
For example: import Lianxi test1. demo. visit
It's not allowed to write like this
Import modules and find the order of modules: project directory – Python installation directory (lib) -- pip install third-party module (site package)
You can import built-in and third-party modules directly into sth without writing a path
When importing custom modules, you usually start from the bottom of the root directory, and you need to write the path
You can directly import the Python module into the site package directory instead of the custom module directory
4, os module
import os# mainly deals with system related operations
Mainly use OS Path to handle system path related operations
import os.path or from os import path
# Get the absolute path of the file (use the most, because the absolute path will not change) print(os.path.abspath(__file__)) print(__file__) print(__name__) ''' D:\Program Files (x86)\PyCharm\workspace\Lemon\0714.py D:/Program Files (x86)/PyCharm/workspace/Lemon/0714.py __main__ '''
Absolute path: start from the system's drive letter or system root directory
Relative path: a way of saying that one path is relative to another
__ file__: The path of the current file may be absolute or relative
__ name__: Module name, no path
import os # Get file absolute path file_path = os.path.abspath(__file__) print("file_path:",file_path) # Get the upper level path dir_name = os.path.dirname(file_path) # Path splicing print(os.path.join(dir_name,'demo.txt')) ''' file_path: D:\Program Files (x86)\PyCharm\workspace\Lemon\0714.py D:\Program Files (x86)\PyCharm\workspace\Lemon\demo.txt '''
Note: all the obtained paths are only the representation of a path, and do not mean that the file or path really exists
# Get current working directory print(os.getcwd()) # Create a new directory (folder) print(os.mkdir('class_2')) # Determine whether a path exists print(os.path.exists(os.getcwd())) # Determine whether the current path is a file print(os.path.isfile(os.getcwd())) # Determine whether the current path is a directory print(os.path.isdir(os.getcwd()))
'''
D:\Program Files (x86)\PyCharm\workspace\Lemon
None
True
False
True
'''