[英]Location of the added files after the executable file is generated by pyinstaller build
I built an executable file for my python application using pyinstaller. 我使用pyinstaller为python应用程序构建了一个可执行文件。 I added two folders (data and audio samples) by using --add-data command.
我使用--add-data命令添加了两个文件夹(数据和音频样本)。 The executable is working.
可执行文件正在运行。 But where can I find the two data folders I added.
但是在哪里可以找到我添加的两个数据文件夹。 Aren't they put into the dist folder with executable file?
他们不是将可执行文件放入dist文件夹吗?
When you add a data to PyInstaller it would bring your data and extract it in one-file
mode in temp
folder (eg on Windows it would be something like C:\\Users\\<User>\\Appdata\\local\\temp\\_MEI60482\\
) so you need to change your code to open your files from that directory. 当您将数据添加到PyInstaller时,它将带入您的数据并将其以
one-file
模式提取到temp
文件夹中(例如,在Windows上,它将类似于C:\\Users\\<User>\\Appdata\\local\\temp\\_MEI60482\\
)因此,您需要更改代码才能从该目录打开文件。 A good practice is to use this function in your code to retrieve your data. 一个好的做法是在代码中使用此功能来检索数据。 When running executable
sys._MEIPASS
would be equal to the PyInstaller temp folder. 运行可执行文件
sys._MEIPASS
将等于PyInstaller临时文件夹。
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
Then you can use it with something like source = resource_path("audio.zip")
. 然后,您可以将其与
source = resource_path("audio.zip")
类的东西一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.