上文虽然讲解了导包和导模块的基本操作,但是实际使用的时候,还是会很困惑,到底该怎么用。本文将结合具体的实例,展示Python导模块的基本操作。
1、包、模块的代码和结构
首先新建一个mypackage
包,包下面有三个文件(模块),其中一个是__init__.py
文件。结构树如下:
mypackage/
__init__.py
(空文件)mymodule.py
another_module.py
mypackage/mymodule.py:
# mypackage/mymodule.py
my_variable = 42
def my_function():
print("This is a function defined in mymodule.py")
class MyClass:
def __init__(self, name):
self.name = name
def say_hello(self):
print(f"Hello, my name is {self.name}")
mypackage/another_module.py:
# mypackage/another_module.py
another_variable = 100
def another_function():
print("This is a function defined in another_module.py")
class AnotherClass:
def __init__(self, description):
self.description = description
def introduce(self):
print(f"My description is: {self.description}")
二、导包示例:main.py文件
# main.py
# 导入 mypackage 包中的 mymodule 模块
import mypackage.mymodule
# 使用 mymodule 模块中的变量、函数和类
print(mypackage.mymodule.my_variable) # 输出: 42
mypackage.mymodule.my_function() # 输出: This is a function defined in mymodule.py
obj = mypackage.mymodule.MyClass("Alice")
obj.say_hello() # 输出: Hello, my name is Alice
# 导入 mypackage 包中的 another_module 模块
import mypackage.another_module
# 使用 another_module 模块中的变量、函数和类
print(mypackage.another_module.another_variable) # 输出: 100
mypackage.another_module.another_function() # 输出: This is a function defined in another_module.py
another_obj = mypackage.another_module.AnotherClass("A description of this object")
another_obj.introduce() # 输出: My description is: A description of this object
# 使用 from ... import ... 语法导入特定的内容
from mypackage.mymodule import MyClass
from mypackage.another_module import AnotherClass
# 现在可以直接使用这些内容,而不需要加上模块名作为前缀
new_obj = MyClass("Bob")
new_obj.say_hello() # 输出: Hello, my name is Bob
another_new_obj = AnotherClass("Another description")
another_new_obj.introduce() # 输出: My description is: Another description
3、更多示例(可不看)
示例 1: 导入整个模块
# main.py
# 导入 mypackage 包中的 mymodule 模块
import mypackage.mymodule
# 使用时需要加上模块名作为前缀
mypackage.mymodule.my_function()
示例 2: 从模块中导入特定的函数或类
# main.py
# 从 mypackage.mymodule 模块中导入特定的函数和类
from mypackage.mymodule import my_function, MyClass
# 现在可以直接使用这些函数和类,而不需要加上模块名作为前缀
my_function()
obj = MyClass("Alice")
obj.say_hello()
示例 3: 使用 as
关键字为导入的内容指定别名
# main.py
# 使用别名来避免命名冲突或简化代码
from mypackage.mymodule import MyClass as MyCustomClass
from mypackage.another_module import another_function as af
# 使用别名
obj = MyCustomClass("Bob")
obj.say_hello()
af()
示例 4: 导入包中的多个模块
# main.py
# 分别导入包中的两个模块
import mypackage.mymodule
import mypackage.another_module
# 使用时需要加上模块名作为前缀
mypackage.mymodule.my_function()
mypackage.another_module.another_function()
示例 5: 在 __init__.py
中设置包的 __all__
属性
如果你希望在从包级别进行 from package import *
时,只导入特定的模块或内容,可以在 __init__.py
文件中设置 __all__
属性。
mypackage/init.py(设置 __all__
):
# mypackage/__init__.py
__all__ = ['mymodule', 'another_module']
# 注意:这不会真正导入模块,只是告诉解释器哪些模块应该被导入
# 当使用 from mypackage import * 时,只会导入 mymodule 和 another_module
然后,在 main.py
中:
# main.py
# 导入 mypackage 包中的所有内容(仅限于 __all__ 中列出的模块)
from mypackage import *
# 现在可以直接使用 mymodule 和 another_module,但通常不推荐这样做,因为它可能导致命名冲突
mymodule.my_function()
another_module.another_function()
示例 6: 使用相对导入来引用其他模块或包
# 在 mypackage/some_module.py 中
# 相对导入另一个模块
from . import another_module
# 如果要导入上一级包中的模块(假设有的话),可以这样写
from .. import some_other_package