在python脚本和exe文件运行时,可以通过在sys
中是否存在变量frozen
- python 脚本: getattr(sys, ‘frozen’, False) = False
- Exe文件: getattr(sys, ‘frozen’, False) = True
代码示例
import os
import sys
def get_root_path():
# determine if application is a script file or frozen exe
if getattr(sys, 'frozen', False):
return os.path.abspath(sys._MEIPASS)
else:
return os.path.dirname(os.path.abspath(__file__))