亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> Python

Python 模块导入实战指南:从基础到包管理

什么是 Python 模块?

模块是单个 Python 文件,可包含变量、函数、类。文件名为 guru99.py 时,模块名为 guru99。模块将大程序拆分为逻辑片段,保持代码组织有序、可复用。

为什么要导入模块?

使用其他文件定义的代码必须导入。导入获得模块函数、类、变量访问权,类似 JavaScript、Java、Ruby 的 import 关键字。

创建与导入模块

通过简单示例演示。

文件夹结构:

modtest/
  test.py
  display.py

1. 创建 test.py:

def display_message():
    return 'Welcome to Guru99 Tutorials!'

2. 创建 display.py 并导入 test:

import test
print(test.display_message())

运行 display.py 输出:

Welcome to Guru99 Tutorials!

从模块导入类

假设 Car.py 定义汽车类:

class Car:
    brand_name = 'BMW'
    model = 'Z4'
    manu_year = '2020'
    def __init__(self, brand_name, model, manu_year):
        self.brand_name = brand_name
        self.model = model
        self.manu_year = manu_year
    def car_details(self):
        print('Car brand is', self.brand_name)
        print('Car model is', self.model)
        print('Car manufacture year is', self.manu_year)
    def get_car_brand(self):
        print('Car brand is', self.brand_name)
    def get_car_model(self):
        print('Car model is', self.model)

在 display.py 中使用:

import Car
car_det = Car.Car('BMW', 'Z5', 2020)
print(car_det.brand_name)
car_det.car_details()
car_det.get_car_brand()
car_det.get_car_model()

输出:

BMW
Car brand is BMW
Car model is Z5
Car manufacture year is 2020
Car brand is BMW
Car model is Z5

使用 from 导入特定项

仅需少数函数或变量时直接导入:

from test import display_message
print(display_message())

尝试使用未导入名称会抛出 NameError:

from test import display_message
print(display_message1())

导入模块全部内容

用 import test 并加模块前缀,或用 from test import * 将所有名字导入当前命名空间。后者方便但可能污染命名空间。

理解 dir()

内置 dir() 列出对象所有属性,包括模块定义的:

import Car
print(dir(Car))

标准库示例:

import json
print(dir(json))

包的使用

包是包含 __init__.py 文件和一个或多个模块的目录。例如:

mypackage/
  __init__.py
  module1.py
  module2.py
  module3.py

module1.py 中:

def mod1_func1():
    print('Welcome to Module1 function1')
def mod1_func2():
    print('Welcome to Module1 function2')
# …

使用别名导入:

import mypackage.module1 as mod1
mod1.mod1_func1()
mod1.mod1_func2()

Python 模块搜索路径

Python 解析导入时按顺序检查:

  1. 当前工作目录。
  2. 内置模块。
  3. sys.path 列出的目录。

查看路径:

import sys
print(sys.path)

使用模块别名

给模块起短名提升可读性:

import test as t
print(t.display_message())
print(t.my_name)

绝对导入 vs 相对导入

多模块项目中,可用完整路径(绝对)或相对当前文件(相对)引用模块。

绝对导入示例

from package1.module1 import myfunc1
# 或
import package1.module1
package1.module1.myfunc1()

相对导入示例

from .module1 import myfunc1
# 或
from .subpkg.module3 import myfunc3

绝对导入清晰、移动项目不易出错,但可能冗长。相对导入简洁,但在大项目中追踪较难。

核心要点

Python

  1. Python模块:创建、导入与高级用法
  2. 精通 Python time 模块:函数、结构体与实战示例
  3. Python print() 函数完全指南:基础用法、空行打印与自定义结束符
  4. Python str.count() 完全指南:统计字符与子串出现次数实战
  5. Python format() 字符串格式化完全指南:占位符、格式化规范、类与字典应用
  6. Python 字符串 find() 方法完全指南:语法、参数、替代方案与计数技巧
  7. 精通 Python Lambda 函数:实用示例与最佳实践
  8. Python round() 函数详解与实战示例
  9. 精通 Python map() 函数:语法、示例与最佳实践
  10. Python timeit():精准测量代码执行时间与实战示例
  11. Python collections.Counter:高效计数、更新与算术运算
  12. 理解 Python type() 与 isinstance():实战示例详解