如何解決Python中ModuleNotFoundError錯誤
模塊對于開發(fā) Python 程序很重要。 使用模塊,我們可以分離代碼庫的不同部分以便于管理。
使用模塊時,了解它們的工作方式以及如何將它們導入我們的代碼非常重要。 如果沒有這種理解或錯誤,我們可能會遇到不同的錯誤。
此類錯誤的一個示例是 ModuleNotFoundError。 在本文中,我們將討論在 Python 中解決 ModuleNotFoundError 的方法。
使用正確的模塊名稱解決 Python 中的 ModuleNotFoundError
讓我們用兩個文件創(chuàng)建一個簡單的 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)
現(xiàn)在,讓我們運行 index.py。 我們的代碼執(zhí)行的輸出如下。
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。 編寫模塊名稱時要小心。
現(xiàn)在,讓我們更正它并運行我們的代碼。
import file
studentOne = file.Student("Isaac", "Asimov")
print(studentOne.lastName)
代碼的輸出:
Asimov
此外,我們可以使用 from 關鍵字重寫 import 語句并僅導入 Student 類。 這對于我們不想導入模塊中存在的所有函數(shù)、類和方法的情況很有用。
from file import Student
studentOne = Student("Isaac", "Asimov")
print(studentOne.lastName)
我們將得到與上次相同的輸出。
使用正確的語法解決 Python 中的 ModuleNotFoundError
當我們在導入另一個模塊時使用錯誤的語法時,特別是在單獨目錄中使用模塊時,我們可能會得到 ModuleNotFoundError。
讓我們使用與上一節(jié)相同但有一些擴展的代碼來創(chuàng)建一個更復雜的代碼庫。 要創(chuàng)建此代碼庫,我們需要以下項目結(jié)構。
Project/
data/
file.py
welcome.py
index.py
有了這個結(jié)構,我們就有了一個包含文件和歡迎模塊的數(shù)據(jù)包。
在 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 類和函數(shù) 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
該代碼嘗試直接使用點運算符導入函數(shù) printWelcome 和類 Student,而不是使用 from 關鍵字或 __init__.py 來輕松綁定子模塊。 通過這樣做,我們有一個 ModuleNotFoundError 拋給我們。
讓我們使用正確的 import 語句語法來防止 ModuleNotFoundError 并直接導入函數(shù)和類。
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
我們可以將數(shù)據(jù)包中的模塊(文件和歡迎)綁定到它的父命名空間。 為此,我們需要 __init__.py 文件。
在 __init__.py 文件中,我們導入包內(nèi)的所有模塊及其函數(shù)、類或?qū)ο?,以便于管理?/p>
from .file import Student from .welcome import printWelcome
現(xiàn)在,我們可以更簡潔地編寫我們的 index.py 并很好地綁定到父命名空間 data。
from data import Student, printWelcome
welcome = printWelcome("Lagos")
studentOne = Student("Isaac", "Asimov")
print(welcome)
print(studentOne.firstName)
輸出將與上次代碼執(zhí)行相同。
為防止出現(xiàn) ModuleNotFoundError 錯誤消息,請確保您沒有錯誤的導入語句或印刷錯誤。
到此這篇關于如何解決Python中ModuleNotFoundError錯誤的文章就介紹到這了,更多相關Python ModuleNotFoundError錯誤內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Python報錯:ModuleNotFoundError的解決辦法
- python命令行運行報錯ModuleNotFoundError:No module named‘XXX‘找不到自定義模塊
- Python解決ModuleNotFoundError: No module named 'PIL'的問題
- Python?ModuleNotFoundError:?No?module?named?‘xxx‘可能的解決方案大全
- Python中ModuleNotFoundError錯誤的問題解決
- Python報錯ModuleNotFoundError: No module named ‘tensorboard‘的解決方法
- Python中ModuleNotFoundError模塊未找到的解決方法
相關文章
Python實現(xiàn)for循環(huán)倒序遍歷列表
這篇文章主要介紹了Python實現(xiàn)for循環(huán)倒序遍歷列表,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
python virtualenv虛擬環(huán)境配置與使用教程詳解
這篇文章主要介紹了python virtualenv虛擬環(huán)境配置與使用教程詳解,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
Python中等待5秒并暫停執(zhí)行的方法總結(jié)
Python 具有各種功能和庫來創(chuàng)建交互式應用程序,用戶可以在其中提供輸入和響應, 我們可以創(chuàng)建需要暫停應用程序執(zhí)行的情況,本文主要和大家分享三個Python 中等待 5 秒并暫停執(zhí)行的方法,有需要的可以參考下2023-10-10
關于Python 實現(xiàn)tuple和list的轉(zhuǎn)換問題
這篇文章主要介紹了Python 實現(xiàn)tuple和list的轉(zhuǎn)換,文中介紹了list(列表)和tuple(元組)共同點和區(qū)別,結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-05-05

