python pyinstaller 打包后 os路径获取不正确解决方案

gen_path = os.path.dirname(os.path.realpath(sys.argv[0]))
#以上这句获取路径就正确了。

延伸阅读:html

os.path.dirname(__file__)返回脚本的路径,可是须要注意一下几点:python

一、必须是实际存在的.py文件,若是在命令行执行,则会引起异常NameError: name '__file__' is not definedlinux

 

os.path.abs()与os.path.realpath()的一点区别

相同点

1. 二者都是返回绝对路径,若是参数path为空,则返回当windows

不一样点

os.path.abspath()返回绝对路径,但不处理符号连接(注意linux中的符号连接不一样于windows中的快捷方式)spa

os.path.realpath()先处理路径中的符号连接,再返回绝对路径前文件所在目录的绝对路径命令行

 

延伸点参考连接:http://www.mamicode.com/info-detail-2362471.htmlcode