python 项目,有一个引用报错
相对路径引用问题
![3d49c961dfb1fa038b0005c5e2f609bb.png](https://i-blog.csdnimg.cn/blog_migrate/60b969b7a558aa60734b6566ef6dc4f3.jpeg)
解决:
__main__.py 文件中,
用
import os, sys
dir_path = os.path.dirname(os.path.realpath(__file__))
parent_dir_path = os.path.abspath(os.path.join(dir_path, os.pardir))
sys.path.insert(0, parent_dir_path)
替换
import sys
if __package__ is None and not hasattr(sys, 'frozen'):
# direct call of __main__.py
import os.path
path = os.path.realpath(os.path.abspath(__file__))
sys.path.insert(0, os.path.dirname(os.path.dirname(path)))
上面更改的意思是,
直接跑源代码,
等价于
import sys
# direct call of __main__.py
import os.path
path = os.path.realpath(os.path.abspath(__file__))
sys.path.insert(0, os.path.dirname(os.path.dirname(path)))
frozen
not hasattr(sys, 'frozen')
不是通过打包,
去跑源代码
import sys
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
print('running in a PyInstaller bundle')
else:
print('running in a normal Python process')
# 不是通过打包,
# 去跑源代码