程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python foundation - module (Standard Library)

編輯:Python

1、 modular

1.1 What is a module

        python Standard libraries and built-in modules are not a concept , The standard library contains built-in modules , Built in modules belong to the standard library .

         For example, our common basic data types belong to python Standard library , But not built-in modules .

1.2 Module classification

  • Built-in module
  • Third-party module
  • Custom module

2、 The import module

2.1 Import a single module

  • import module         Import all members in the specified module ( Including variable 、 function 、 Class etc.
  • import module as Alias     names
  • from module import *     Only the members specified in the module will be imported

2.1 Import multiple modules

Method 1 :import os,time,sys Each module is separated by English commas .

Method 2 :import os

import time

import sys

Be careful : It is recommended to use method 2 to import multiple modules , Import module sequence : Built-in module ----> Third-party module ------> Custom module ;

The module is loaded into memory the first time it is imported , Follow up import Statement only adds a reference to a module object that has been loaded in large memory . Third party modules , A custom module may call a built-in module , therefore , When we import modules , You should first import the built-in module of the interpreter , Then import the third-party module , Finally, import the custom module .

3、 Import module failure reason and solution

3.1 Error message

Sometimes we will encounter import module failure , Error message :ModuleNotFoundError: No module named ' Module name '.

3.2 Cause analysis

To solve this problem , We need to know Python The process of finding module files by the interpreter :

  • In the current directory , That is, find the directory where the currently executed program file is located ;
  • To PYTHONPATH( environment variable ) Find... In each of the directories below ;
  • To Python Find in the default installation directory .

3.3 Solution

After knowing the reason , solve “Python Specified module not found ” The way to do this is 3 Kind of , Namely :

  • towards sys.path The full path of the storage location of the temporarily added module file in ;
  • Place the module in sys.path The module already contained in the variable is in the loading path ;
  • Set up path System environment variable .


reference:

What is a module ,Python Modular programming ( Entry required )

Python Standard library — Python 3.8.12 file


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved