欢迎访问宙启技术站
智能推送

「Python」模块和包的导入及使用

发布时间:2023-06-22 04:02:24

Python作为一种高级编程语言,在开发中有许多需要复用的代码部分,这时候就需要使用模块和包。模块是指一个文件,包是指一个文件夹,内含多个模块文件和一个 __init__.py 的文件,用来实现模块的组织和管理,能够提高代码可读性和维护性。本文将简要介绍Python中模块和包的导入以及使用方法。

一、模块导入方法

1. import 语句

在Python中,使用import可以导入一个模块或多个模块,语法如下:

import module_name1[, module_name2[,... module_nameN]]

例如:

import math  # 导入math模块
import random,os  # 导入random和os两个模块

上述代码可以导入math、random和os三个模块。

2. from...import 语句

Python中还可以使用from...import语句来导入模块中的部分内容,语法如下:

from module_name import name1[, name2[,... nameN]]

例如:

from math import pi  # 导入math模块中的pi变量
from os import getcwd, chdir  # 导入os模块中的getcwd和chdir函数

上述代码可以导入math模块中的pi变量和os模块中的getcwd和chdir函数。

3. from...import* 语句

Python中还可以使用from...import*语句来导入模块中的所有内容,语法如下:

from module_name import *

例如:

from math import *  # 导入math模块中的所有内容

上述代码可以导入math模块中的所有内容。

二、包的导入方法

Python包是一个包含多个模块的文件夹,可以使用import语句来导入包中的模块。例如:

import my_package.my_module

其中,my_package是包名,my_module是模块名。

当然也可以使用from...import语句来导入包中的模块中的内容。例如:

from my_package.my_module import my_function

其中,my_function是模块中的函数名。

三、模块和包的常用命名规范

Python中,模块和包的命名应该符合以下规范:

1. 模块和包的命名应该尽量简短,使用小写字母和下划线的组合。

2. 在包中,需要创建一个 __init__.py 的文件来标识这个文件夹是一个包。

3. 在一个模块文件中,命名应该符合下划线分隔的小写字母的格式,例如:my_module.py。

4. 每个包都应该有一个 __init__.py 文件,可以在这个文件中添加初始化代码。

5. 在 __init__.py 文件中,需要指定包中需要导出的模块,这样才可以使用 from my_package import * 语句导入所有模块。

四、模块和包的常用使用方法

1. 使用 dir() 函数查看模块或包包含哪些内容。

例如:

import math
print(dir(math))

输出结果如下:

['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']

上述代码使用 dir() 函数查看了 math 模块中所有可用的属性和函数。

2. 使用 help() 函数查看模块或包的使用方法。

例如:

import math
help(math)

等价于:

from pydoc import help
help(math)

输出结果如下:

Help on module math:

NAME
    math

MODULE REFERENCE
    https://docs.python.org/3.8/library/math.html

The following documentation is automatically generated from the Python
source files.  It may be incomplete, incorrect or include features that
are considered implementation detail and may vary between Python
implementations.  When in doubt, consult the module reference at the
location listed above.

DESCRIPTION
    This module provides access to the mathematical functions
    defined by the C standard.

FUNCTIONS
    acos(...)
        acos(x)
        
        Return the arc cosine (measured in radians) of x.

    acosh(...)
        acosh(x)
        Return the inverse hyperbolic cosine of x....

上述代码使用 help() 函数查看了 math 模块的使用方法。

3. 在代码中使用模块或包中的代码。

例如:

import math

def circle_area(r):
    return math.pi * r * r

if __name__ == '__main__':
    print(circle_area(2))

上述代码在代码中使用了 math 模块中的 pi 常量。运行结果如下:

12.566370614359172

总结

本文介绍了 Python 中模块和包的导入和使用方法,这些方法都是 Python 中非常基础和重要的知识点。通过对模块和包的学习,可以使我们更方便地管理代码和提高代码的可读性和复用性。