解决 Python ModuleNotFoundError 错误
模块对于开发 Python 程序很重要。 使用模块,我们可以分离代码库的不同部分以便于管理。
使用模块时,了解它们的工作方式以及如何将它们导入我们的代码非常重要。 如果没有这种理解或错误,我们可能会遇到不同的错误。
此类错误的一个示例是 ModuleNotFoundError。 在本文中,我们将讨论在 Python 中解决 ModuleNotFoundError 的方法。
使用正确的模块名称解决 Python 中的 ModuleNotFoundError
让我们用两个文件创建一个简单的 Python 代码库,index.py 和 file.py,我们将 file.py 导入到 index.py 文件中。 这两个文件都在同一目录中。
file.py 文件包含以下代码。
class Student():
def __init__(self, firstName, lastName):
self.firstName = firstName
self.lastName = lastName
index.py 文件包含以下代码。
import fiIe
studentOne = fiIe.Student("Isaac", "Asimov")
print(studentOne.lastName)
现在,让我们运行 index.py。 我们的代码执行的输出如下。
Traceback (most recent call last):
File "c:\Users\akinl\Documents\Python\index.py", line 1, in <module>
import fiIe
ModuleNotFoundError: No module named 'fiIe'
我们有一个 ModuleNotFoundError。 如果仔细观察,您会注意到 import 语句有一个拼写错误,其中 file 被写为 file,l 被大写的 I 替换了。
因此,如果我们使用了错误的名称,就会抛出 ModuleNotFoundError。 编写模块名称时要小心。
现在,让我们更正它并运行我们的代码。
import file
studentOne = file.Student("Isaac", "Asimov")
print(studentOne.lastName)
代码的输出:
Asimov
此外,我们可以使用 from
关键字重写 import
语句并仅导入 Student 类。 这对于我们不想导入模块中存在的所有函数、类和方法的情况很有用。
from file import Student
studentOne = Student("Isaac", "Asimov")
print(studentOne.lastName)
我们将得到与上次相同的输出。
使用正确的语法解决 Python 中的 ModuleNotFoundError
当我们在导入另一个模块时使用错误的语法时,特别是在单独目录中使用模块时,我们可能会得到 ModuleNotFoundError。
让我们使用与上一节相同但有一些扩展的代码来创建一个更复杂的代码库。 要创建此代码库,我们需要以下项目结构。
Project/
data/
file.py
welcome.py
index.py
有了这个结构,我们就有了一个包含文件和欢迎模块的数据包。
在 file.py 文件中,我们有以下代码。
class Student():
def __init__(self, firstName, lastName):
self.firstName = firstName
self.lastName = lastName
在 welcome.py 中,我们有以下代码。
def printWelcome(arg):
return "Welcome to " + arg
index.py 包含尝试导入文件和欢迎并使用 Student 类和函数 printWelcome 的代码。
import data.welcome.printWelcome
import data.file.Student
welcome = printWelcome("Lagos")
studentOne = Student("Isaac", "Asimov")
print(welcome)
print(studentOne.firstName)
运行index.py的输出:
Traceback (most recent call last):
File "c:\Users\akinl\Documents\Python\index.py", line 1, in <module>
import data.welcome.printWelcome
ModuleNotFoundError: No module named 'data.welcome.printWelcome'; 'data.welcome' is not a package
该代码尝试直接使用点运算符导入函数 printWelcome 和类 Student,而不是使用 from 关键字或 __init__.py
来轻松绑定子模块。 通过这样做,我们有一个 ModuleNotFoundError 抛给我们。
让我们使用正确的 import 语句语法来防止 ModuleNotFoundError 并直接导入函数和类。
from data.file import Student
from data.welcome import printWelcome
welcome = printWelcome("Lagos")
studentOne = Student("Isaac", "Asimov")
print(welcome)
print(studentOne.firstName)
代码的输出:
Welcome to Lagos
Isaac
我们可以将数据包中的模块(文件和欢迎)绑定到它的父命名空间。 为此,我们需要 __init__.py
文件。
在 __init__.py
文件中,我们导入包内的所有模块及其函数、类或对象,以便于管理。
from .file import Student
from .welcome import printWelcome
现在,我们可以更简洁地编写我们的 index.py 并很好地绑定到父命名空间 data。
from data import Student, printWelcome
welcome = printWelcome("Lagos")
studentOne = Student("Isaac", "Asimov")
print(welcome)
print(studentOne.firstName)
输出将与上次代码执行相同。
为防止出现 ModuleNotFoundError 错误消息,请确保您没有错误的导入语句或印刷错误。
相关文章
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串