Pyinstaller 添加数据文件 [英] Pyinstaller adding data files
问题描述
我正在努力使用 pyinstaller.每当我使用 kivy GUI 和 .kv 文件构建此特定脚本并在构建后运行 .exe 时,我都会收到一个致命错误:
I'm struggling with pyinstaller. Whenever I build this specific script with a kivy GUI and a .kv file, and run the .exe after the build, I get a fatal error:
IOError: [Errno 2] No such file or directory: 'main.kv'
我尝试使用 --add-data 添加 .kv 文件以及 mdb 和 dsn 文件(用于 pypyodbc),但出现错误:unrecognized arguments: --add-data'main.kv'
.(对于提到的其他文件,还有更多 --add-data 参数.)
I've tried adding the .kv file, as well as a mdb and dsn file (for pypyodbc) using --add-data, but I get an error: unrecognized arguments: --add-data'main.kv'
. (There were more --add-data arguments for the other files mentioned.)
是否有任何解决方案或替代方法?
Are there any solutions for this or maybe alternative methods?
推荐答案
正如其他人(@Anson Chan,@schlimmchen)所说:
As others (@Anson Chan, @schlimmchen) have said:
如果你想添加一些额外的文件,你应该使用 添加数据文件.
If you want to add some extra files, you should use Adding Data Files.
- 命令行:将参数添加到
--add-data
- 规范文件:将参数添加到
datas=
- 在第一次运行
pyinstaller
时生成.- 稍后您可以编辑您的
*.spec
文件. - 然后运行
pyinstaller
将直接使用你的*.spec
文件.
- Command Line: add parameter to
--add-data
- Spec file: add parameter to
datas=
- Generated when running
pyinstaller
the first time.- Then later you can edit your
*.spec
file. - Then running
pyinstaller
will directly use your*.spec
file.
--add-data
或datas=
中的参数:--add-data
:- 格式:
{source}{os_separator}{destination}
os_separator
:- Windows:
;
- Mac/Linux/Unix:
:
- 逻辑:
source
:单个或多个文件的路径,支持glob 语法.告诉 PyInstaller 在哪里可以找到文件.目的地
文件或文件:在运行时将包含您的源文件的目标文件夹.* NOTE:NOTE 目标文件名.- 文件夹:目标文件夹路径,与目标根目录RELATIVE,不是绝对路径.
- Logic:
source
: path to single or multiple files, supporting glob syntax. Tells PyInstaller where to find the file(s).destination
file or files: destination folder which will contain your source files at run time. * NOTE: NOT the destination file name.- folder: destination folder path, which is RELATIVE to the destination root, NOT an absolute path.
- 单个文件:
'src/README.txt:.'
- 多个文件:
'/mygame/sfx/*.mp3:sfx'
- 文件夹:
/mygame/data:data'
- 格式:列表或元组.
- 示例:请参阅以下内容.
added_files = [ ( 'src/README.txt', '.' ), ( '/mygame/data', 'data' ), ( '/mygame/sfx/*.mp3', 'sfx' ) ] a = Analysis(... datas = added_files, ... )
你的情况
对于您的(Windows 操作系统),这里是:
Your case
For your (Windows OS) here is:
--add-data
在命令行中pyinstaller -F --add-data "main.kv;."yourtarget.py
--add-data
in command linepyinstaller -F --add-data "main.kv;." yourtarget.py
或者:
datas=
,请参见以下内容:
yourtarget.spec
文件中的datas=
inyourtarget.spec
file, see following:
a = Analysis(... datas = ["main.kv", "."], ... )
这篇关于Pyinstaller 添加数据文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Windows:
- 格式:
- Then later you can edit your
- Generated when running
- 稍后您可以编辑您的
- 在第一次运行