【发布时间】:2019-02-24 19:57:37
【问题描述】:
我有这个代码:
def _read_config(self):
config = configparser.ConfigParser()
config.sections()
# I tried
path_main = os.path.dirname(os.path.realpath(__file__))
# and this after create exec file with pyinstaller nothing changed
path_main = os.getcwd()
print(path_main)
file = os.path.join(path_main, "config.ini")
print(file)
config.read(file)
return config
当我使用带有python gui.py 的终端在 MacOS 中运行代码时,它会打印:
/Users/telos/Desktop/Telos-Monitor-Tool/client
/Users/telos/Desktop/Telos-Monitor-Tool/client/config.ini
但是当我执行pyinstaller --onefile --windowed gui.py 时,我收到了 1 个应用程序文件,当我运行它时,我得到了这个:
/Users/telos
/Users/telos/config.ini
但是一个文件 app 和 ``gui.py` 在同一个目录下。
所以我有一个错误,因为 Python 解析器找不到 config.ini。
在重新创建应用程序后建议我使用 print(QtCore.QCoreApplication.applicationDirPath()),我有 2 个文件 1 gui.app,2-nd gui.exec。 gui.exec 找到 config.ini 并一切正常,但 gui.app 无法发送错误。
知道是什么问题吗?
【问题讨论】:
-
我没有在您的代码中看到对
os.getcwd()的调用。 -
使用
os.getcwd()更改,但尝试使用getcwd(),这样,一样 -
工作目录不是脚本/可执行文件所在的目录,而是你运行它时所在的目录。如果您在运行它时位于 /Users/telos(任一版本),那么它的工作目录就是这样。
-
如何检查应用程序的位置?来自 Python?
-
你读过这个问题了吗,
path_main =os.path.dirname(os.path.realpath(__file__))我试过了,和getcwd一样,在我为mac创建exec文件后应用程序找不到配置并返回/Users/telos/config.ini但配置/Users/telos/Desktop/Telos-Monitor-Tool/client/config.ini
标签: python python-3.x macos pyinstaller