使用 onefile 选项在 Pyinstaller 中添加数据文件 [英] Adding a data file in Pyinstaller using the onefile option

查看:117
本文介绍了使用 onefile 选项在 Pyinstaller 中添加数据文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像添加到 Pyinstaller 生成的一个文件中.我读过很多这样的问题/论坛一个一个,但仍然无法正常工作.

I'm trying to add an image to the one file produced by Pyinstaller. I've read many questions/forums like this one and that one and still it's not working.

我知道,对于一个文件操作,Pyinstller 会生成一个可以由 sys.MEIPASS 访问的临时文件夹.但是我不知道我应该在脚本中的哪个位置添加这个 sys.MEIPASS.

I know that for one file operation, Pyinstller produce a temp folder that could be reached by sys.MEIPASS. However I don't know where exactly in my script I should add this sys.MEIPASS.

请显示以下内容:

1- 在哪里以及如何 sys.MEIPASS 应该添加?在python脚本中,还是在spec文件中?

1- Where and how sys.MEIPASS should be added? In the python script, or in the spec file?

2- 要使用的确切命令是什么?我试过了

2- What is the exact command to use? I've tried

pyinstaller --onefile --windowed --add-data="myImag.png;imag" myScript.py

pyinstaller --onefile --windowed myScript.py

然后将 ('myImag.png','imag') 添加到规范文件中,然后运行 ​​

and then add ('myImag.png','imag') to the spec file and then run

pyinstller myScript.spec

没有一个有效.

注意:我在 windows 7 下有 python 3.6

Note: I have python 3.6 under windows 7

推荐答案

当使用 PyInstaller 打包成单个文件时,运行 .exe 会将所有内容解压到 TEMP 目录中的一个文件夹中,运行脚本,然后丢弃临时文件.临时文件夹的路径随着每次运行而变化,但对其位置的引用添加到 sys 作为 sys._MEIPASS.

When packaged to a single file with PyInstaller, running the .exe will unpack everything to a folder in your TEMP directory, run the script, then discard the temporary files. The path of the temporary folder changes with each running, but a reference to its location is added to sys as sys._MEIPASS.

要利用这一点,当您的 Python 代码读取任何也将打包到您的 .exe 中的文件时,您需要将文件位置更改为位于 sys._MEIPASS 下.换句话说,您需要将其添加到您的 Python 代码中.

To make use of this, when your Python codes reads any file that will also be packaged into your .exe, you need to change the files location to be located under sys._MEIPASS. In other words, you need to add it to your python code.

这是一个示例,使用您引用的链接中的代码在打包到单个文件时将文件路径调整到正确的位置.

Here is an example that using the code from the link you referenced to adjust the file path to correct location when packaged to a single file.

# data_files/data.txt
hello
world

# myScript.py
import sys
import os

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

def print_file(file_path):
    file_path = resource_path(file_path)
    with open(file_path) as fp:
        for line in fp:
            print(line)

if __name__ == '__main__':
    print_file('data_files/data.txt')

使用以下选项运行 PyInstaller 打包文件:

Running PyInstaller with the following options packages the file:

pyinstaller --onefile --add-data="data_files/data.txt;data_files" myScript.py

构建运行正常的myScript.exe,可以打开和读取打包后的数据文件.

builds myScript.exe which runs correctly and can open and read the packaged data file.

这篇关于使用 onefile 选项在 Pyinstaller 中添加数据文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆