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