首页 > 解决方案 > pyinstaller 更改文件路径的值

问题描述

我正在运行以下代码:

DIRECTORY_PATH = os.getcwd()
print(DIRECTORY_PATH)
print(__file__)
print(os.path.abspath(__file__))
input("Press enter to continue ...")

但是,当我使用 pyinstaller 与仅运行 python 脚本后运行时,我会得到不同的结果。有没有办法获取exe所在的目录?

运行 pyinstaller --onefile exe(注意黑色 python shell 后面的蓝色 powershell,其中包含用于运行 exe 的命令)

用python运行python脚本

标签: python-3.xpathpyinstaller

解决方案


我发现的唯一方法是制作一个批处理文件以从同一目录运行程序。

cd %~dp0
start program.exe

这样,即使从技术上讲,因为它来自批处理文件,您也可以获得起始文件的目录路径。


推荐阅读