简体   繁体   English

使用pyinstaller将.py文件和.txt文件合并到.exe文件中

[英]Merging .py file and .txt files into .exe file using pyinstaller

I have a python program that takes in as input two text files.我有一个 python 程序,它接收两个文本文件作为输入。 I have converted this python program(a.py file) to a.exe file using pyinstaller.我已使用 pyinstaller 将此 python 程序(a.py 文件)转换为 a.exe 文件。 The.exe file when run gives FileNotFoundError.运行时.exe 文件给出 FileNotFoundError。 But when the.txt file is copied to the path where.exe is present it works fine.但是,当 .txt 文件被复制到 .exe 所在的路径时,它可以正常工作。 My intention is to not copy the.txt file but bundle the.txt file along with.exe so that the.txt file is inaccessible.我的意图是不复制 .txt 文件,而是将 .txt 文件与 .exe 捆绑在一起,这样 .txt 文件就无法访问。 All.txt file dependencies i want to bundle it with.exe, ultimately there should be just one.exe file, when i run it, it should work the same way as when i run the python program. All.txt 文件依赖项 我想将它与.exe 捆绑在一起,最终应该只有一个.exe 文件,当我运行它时,它应该与我运行 python 程序时的工作方式相同。 Please suggest me ways in achieving the same请建议我实现相同目标的方法

I am new to pyinstaller.我是 pyinstaller 的新手。 I had tried adding the.txt files to data parameter in the.spec file.我曾尝试将 .txt 文件添加到 .spec 文件中的数据参数。 But this fails to copy the text files into the dist folder where my.exe is present.但这无法将文本文件复制到 my.exe 所在的 dist 文件夹中。 But as i have mentioned i just need the.exe file alone.但正如我所提到的,我只需要单独的 .exe 文件。 Even if the.exe file is shared to someone else who do not have access to any of the text files, the.exe must run sccessfully即使将 .exe 文件共享给无权访问任何文本文件的其他人,该 .exe 也必须成功运行

a.datas+=[('D:/Trial/src/readme_text_files/readme1.txt','readme_text_files/readme1.txt','readme_text_files'), ('D:/Trial/src/readme_text_files/readme2.txt','readme_text_files/readme2.txt','readme_text_files'), ] a.datas+=[('D:/Trial/src/readme_text_files/readme1.txt','readme_text_files/readme1.txt','readme_text_files'), ('D:/Trial/src/readme_text_files/readme2.txt', 'readme_text_files/readme2.txt','readme_text_files'), ]

The above code has been added to.spec file, as a result i assume that the readme_text_files must be copied to the folder where.exe is present when you run: pyinstaller spec_filename.spec上面的代码已添加到 .spec 文件中,因此我假设 readme_text_files 必须复制到运行时存在的文件夹中:pyinstaller spec_filename.spec

I wanted a.txt file to be bundled with.exe file, thus i did the following:我希望将 .txt 文件与 .exe 文件捆绑在一起,因此我执行了以下操作:

  1. I edited the.spec file in the following way:我通过以下方式编辑了 .spec 文件:

     a = Analysis(['mainProgram_edited_for_datas.py'], pathex=['D:\\Trial\\src'], binaries=[], datas=[ ('D:/Trial/src/readme_text_files/readme1.txt','readme_text_files'), ('D:/Trial/src/readme_text_files/readme2.txt','readme_text_files'), ], ...........(Rest of the.spec file contents as it is)

    Or simply you can directly include a directory itself like below:或者只是你可以直接包含一个目录本身,如下所示:

     datas=[('C:/Users/njv5kor/eclipse-workspace/Trial/src/readme_text_files/','readme_text_files'), ],
  2. In the python code i added the below code:在 python 代码中,我添加了以下代码:

     def resource_path(relative_path): """ Get absolute path to resource, works for dev and for PyInstaller """ try: base_path = sys._MEIPASS except Exception: base_path = os.path.abspath(".") return os.path.join(base_path, relative_path) file = resource_path("readme_text_files\\readme1.txt")

Basically the pyinstaller bundles the.txt files to the.py file and creates a single.exe For details regarding _MEIPASS plese refer link: https://pyinstaller.readthedocs.io/en/v3.3.1/operating-mode.html#how-the-one-file-program-works基本上,pyinstaller 将 .txt 文件捆绑到 .py 文件并创建单个.exe 有关 _MEIPASS 的详细信息,请参阅链接: https://pyinstaller.readthedocs.io/en/v3.3.1/operating-mode.html#how -the-one-file-program-works

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 PyInstaller 将 .py 和 .txt 文件封装成 .exe 文件 - Encapsulate .py and .txt file into .exe file using PyInstaller 使用 pyinstaller 将 .py 转换为 .exe 文件 - Converting .py to .exe file using pyinstaller 使用Pyinstaller将.py转换为.exe之后,程序无法找到正确的txt文件位置 - After using Pyinstaller to convert .py to .exe the program cant find the correct txt file location 使用pyinstaller将.json文件添加到exe文件 - adding .json files to an exe file using pyinstaller 我想使用 pyinstaller 将 .py 文件转换为 exe 文件 - I want to make .py file into exe file by using pyinstaller 无法使用 pyinstaller 将 py 文件转换为 exe - Unable to convert py files to exe using pyinstaller 如何从 exe 文件(使用 pyinstaller 将 py 文件转换为 exe 生成)中读取 excel 文件? - How to read an excel file from a exe file (which is generated by converting py file into exe using pyinstaller)? 问题:无法使用 pyinstaller、py2exe 将 .py 文件转换为 exe - problem : cant convert .py file to exe , using pyinstaller, py2exe 如何从 PyInstaller .exe 恢复 .py 文件? - How to recover a .py file from PyInstaller .exe? 如何使用pyinstaller将多个python文件编译成单个.exe文件 - How to compile multiple python files into single .exe file using pyinstaller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM