Python foundation-15 module package library

15. Module package library

15.1 basic concepts

                     . To solve this problem, we will split it and put similar functions in the same file. Each file contains relatively little code. In Python, a. py file is called a module

The benefits of using the module are as follows:

  • 1. Improved code maintainability
  • 2. It improves the reusability of code. When a module is written, it can be referenced by other places.
  • 3. The module can also avoid naming conflicts such as function name, variable name and class name. Functions, variable names, class names with the same name can be placed in different modules, but conflicts with built-in function names should be avoided

Although    module solves the naming conflicts of function name, variable name and class name, there will be naming conflicts before the module. How to solve them? To solve the module naming conflict, Python introduces a method to organize modules by directory, which is called package

For example, now there are two py files, which are and , the corresponding module names are surpassa and surpassb. If these two module names conflict with other module names, you can package them to organize them into packages. The method is to select a top-level package name, such as surpass, and store it in the following form


   after the package is introduced, as long as the package name at the top level does not conflict, you can ensure that the module name does not conflict. Then the corresponding module name is changed from the original module name to surpass.surpassA and surpass.surpassB .

1. Package notes: each package directory must exist__ file, otherwise Python will be treated as a normal directory, not a package.
2.__ init__ The content of. Py file can be empty or have corresponding code, which is also a module


  |- config
  |    |-
  |    |-
  |    |-
  |- web

The module names corresponding to the example are as follows:

  • surpassC: surpass.config.surpassC
  • surpassA:surpass.surpassA

   in Python, a library is a collection of modules with related functions. It can be divided into the following three forms:

  • Standard library: Python built-in Library
  • Custom library: self written
  • Third party Library: written or shared by others


pip/pip3 install -U Library name

15.2 import module

   Python has many built-in modules, which can be called normally as long as the installation is completed. The common forms of import modules are as follows:

import module name
 import module name as alias ා when the module name is very long, it can simplify calling the entered module name
 import module name A, module name B, module name C,
from module nameimport function name / class name import the function or class in the specified module
 from module name import * ා import all contents in the specified module, but exclude variables, functions, etc. that start with an underscore

                   sys.path.append (module name), for example:

import sys
# Indicates that the path of the previous layer is added to the sys.path After adding, the interpreter will search the directory where the current module is located and sys.path Specify the path to find the module that needs to be import ed

The rules for importing modules are as follows:

  • 1. The built-in modules are imported first and ahead
  • 2. When importing a module, try to use as little as possible the name of the module from import*
  • 3. do not repeat the first mock exam. The Python will execute only once for multiple imported modules.

15.3 example code

class Person:

    def __init__(self,name,age):

    def getPerInfo(self):
        print(f"name is {self._name},age is {self._age}")

class Student:

    def __init__(self, name, age,classNo):
        self._name = name
        self._age = age

    def getPerInfo(self):
        print(f"name is {self._name},age is {self._age},class NO. is {self._classNo}")

class Worker:

    def __init__(self,name,occupation):

    def getWorkerInfo(self):
        print(f"name is {self._name},he/she is {self._occupation}")

class Teacher():

    def __init__(self,name,classNO):

    def getClassNo(self):
        print(f"teacher's name is {self._name},His/Her's class is {self._classNo}")

def testFunc(name):
    print(f"Hello, {name}")

import person
from mulClass import Student
from mulClass import testFunc

if __name__ == '__main__':

The output is as follows:

name is Surpass,age is 28
name is Surpass,age is 18,class NO. is Class-1
Hello, testFunc

This article is synchronously published on wechat subscription number. If you like my article, you can also pay attention to my wechat subscription number: woaitest, or scan the following QR code to add attention:

Keywords: Python pip

Added by maest on Sun, 31 May 2020 16:39:12 +0300