Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用onefile选项在Pyinstaller中添加数据文件_Python_Python 3.x_Python 3.6_Pyinstaller - Fatal编程技术网

Python 使用onefile选项在Pyinstaller中添加数据文件

Python 使用onefile选项在Pyinstaller中添加数据文件,python,python-3.x,python-3.6,pyinstaller,Python,Python 3.x,Python 3.6,Pyinstaller,我正在尝试将图像添加到Pyinstaller生成的一个文件中。我读过很多这样那样的问题/论坛,但仍然不起作用 我知道,对于一个文件操作,pyinstler会生成一个临时文件夹,可以通过sys.MEIPASS访问该文件夹。但是我不知道我应该在脚本中的什么地方添加这个sys.MEIPASS 请出示以下文件: 1-应在何处以及如何添加sys.MEIPASS?在python脚本中,还是在spec文件中 2-要使用的确切命令是什么?我试过了 pyinstaller --onefile --windowed

我正在尝试将图像添加到Pyinstaller生成的一个文件中。我读过很多这样那样的问题/论坛,但仍然不起作用

我知道,对于一个文件操作,pyinstler会生成一个临时文件夹,可以通过
sys.MEIPASS
访问该文件夹。但是我不知道我应该在脚本中的什么地方添加这个
sys.MEIPASS

请出示以下文件:

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

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

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

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

pyinstller myScript.spec
没有一个奏效


注意:我在windows 7下安装了python 3.6,当使用PyInstaller打包到单个文件时,运行.exe会将所有内容解压缩到临时目录中的文件夹中,运行脚本,然后丢弃临时文件。临时文件夹的路径会随着每次运行而更改,但对其位置的引用会作为
sys.\u MEIPASS
添加到
sys

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

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

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

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

生成myScript.exe,它可以正确运行,并可以打开和读取打包的数据文件。

我使用命令提示符命令而不是.spec

该命令排除shapely,然后将其添加回(我猜这将调用不同的导入过程)。这显示了如何添加文件夹而不仅仅是文件

pyinstaller --clean --win-private-assemblies --onefile --exclude-module shapely --add-data C:\\Python27\\Lib\\site-packages\\shapely;.\\shapely --add-data C:\\Python27\\tcl\\tkdnd2.8;tcl main.py

我尝试更改了python脚本的工作目录,但似乎可以:

import os 
import sys

os.chdir(sys._MEIPASS)
os.system('included\\text.txt')
my pyinstaller命令:

pyinstaller --onefile --nowindow --add-data text.txt;included winprint.py --distpath .

访问临时文件夹的一种更简单的方法是:

bundle_dir = getattr(sys, '_MEIPASS', path.abspath(path.dirname(__file__)))
data_path = os.path.abspath(path.join(bundle_dir, 'data_file.dat'))

从文档中读到的

我认为--添加数据是正确的,而不是--add-date。当然,只是键入错误。但我执行的时候是对的。谢谢,非常感谢!我的头撞在墙上,想知道如何用我的pyinstaller打包数据文件。感谢你提供了一个有效的示例!注意“;”分隔符只能在Windows上使用。对于Linux和Mac,它是“:”,如下所述:
pyinstaller --onefile --nowindow --add-data text.txt;included winprint.py --distpath .
bundle_dir = getattr(sys, '_MEIPASS', path.abspath(path.dirname(__file__)))
data_path = os.path.abspath(path.join(bundle_dir, 'data_file.dat'))