python異常之ModuleNotFoundError: No module named 'test01inner02'


當我們使用sys.path.append(args) 指令向程序中導入模塊時其實本次append操作只是在內存中完成的,如果要永久性的添加需要修改環境變量。

我們發現當我們使用print(sys.path)后返回的是一個列表,其中包含當前文件所在項目的路徑,還有python的默認加載庫,添加只是暫時的。

錯誤調用:

目錄結構:

  A.test0102.py文件

   # coding = utf-8

def sing():
print("happay new year")

B.init.py文件

#import test0102

import os,sys
from . import test0102
'''
this operate is samed to copy test0102 content to this file
'''
C.test0101.py文件(錯誤調用)
# coding = utf-8

import sys,os
#sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))(本句執行完后若注釋掉,就會出現報錯)
import test01inner02
'''
相當與將test0102的代碼加載到此處解釋編譯
'''
test01inner02.test0102.sing()
D.報錯信息:

E.正確調用

# coding = utf-8

import sys,os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import test01inner02
'''
相當與將test0102的代碼加載到此處解釋編譯
'''
test01inner02.test0102.sing()

 

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM