欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python-pyinstaller、打包后获取路径的实例

程序员文章站 2023-12-09 21:29:27
使用pyinstaller可以把.py文件打包为.exe可执行文件,命令为: pyinstaller hello.py 打包后有两个文件夹,一个是dist,另外一个是b...

使用pyinstaller可以把.py文件打包为.exe可执行文件,命令为:

pyinstaller hello.py

打包后有两个文件夹,一个是dist,另外一个是build,可执行文件在dist文件夹里面,但是会有许多依赖是独立文件存在

pyinstaller -f hello.py

使用-f参数后,打包的可执行文件是一个整体,只有一个.exe文件。

获取文件路径的方式有四种,可以在打包成exe文件后,获取.exe文件的当前路径

import sys

import os

print(sys.path[0])

print(sys.argv[0])

print(os.path.dirname(os.path.realpath(sys.executable)))

print(os.path.dirname(os.path.realpath(sys.argv[0])))

在ide界面的执行结果如下图:

python-pyinstaller、打包后获取路径的实例

在exe文件的执行结果为:

python-pyinstaller、打包后获取路径的实例

以上这篇python-pyinstaller、打包后获取路径的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。