PyInstaller 无法添加 .txt 文件 [英] PyInstaller cannot add .txt files

查看:27
本文介绍了PyInstaller 无法添加 .txt 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程(和 Python)还很陌生,而 Stack Overflow 问题/响应系统让我能够解决迄今为止的所有问题.我没有找到任何直接解决我当前问题的帖子,但不得不承认我真的不知道出了什么问题.让我解释一下.

I'm fairly new with programming (and with Python) and the Stack Overflow question/response system allowed me to resolve all my problems until now. I didn't find any post directly addressing my current issue, but have to admit that I don't really know what's wrong. Let me explain.

我正在尝试使用 PyInstaller 制作 *.py 脚本的可执行文件.使用简单的 Python 脚本(使用 --onefile)没有问题,但是当涉及使用其他 *.py*.txt 的更复杂程序时,它不起作用 文件.我知道我需要修改规范文件并尝试了许多替代方法 - 例如添加隐藏文件.

I'm trying to make an executable file of a *.py script using PyInstaller. There's no problem doing it with a simple Python script (using --onefile), but it does not work when it comes to a more complex program that uses other *.py and *.txt files. I know that I need to modify the specification file and tried many alternatives - adding hidden files for instance.

这里是文件:

  • UpdatingStrategy.py(要转换为可执行文件的目标文件)
  • LPRfunctions.py(UpdatingStrategy.py 从此文件导入函数)
  • UpdatingStrategy.py (the target file to transform in executable)
  • LPRfunctions.py (UpdatingStrategy.py imports functions from this file)

以下*.txt文件被UpdatingStrategy.py读取:

  • Strategy_Observ.txt
  • Strategy_Problems.txt
  • Updating_Observ1.txt
  • Updating_Observ2.txt
  • Updating_Problems.txt

我使用的是 Python 3.5 和 Windows 10.如果您需要更多信息,请告诉我.

I'm using Python 3.5 and Windows 10. Tell me if you need extra information.

如何正确使用规范文件并对其进行修改,使其成为UpdatingStrategy.py的可执行文件?

How do I use the specification file properly and modify it in order to make an executable file of UpdatingStrategy.py?

我已经阅读了 PyInstaller 文档,但我缺乏很多关键原则,无法让它发挥作用.

I have read the PyInstaller documentation, but I lack many key principles and I couldn't make it work.

推荐答案

行后

a = Analysis( ... )

添加

a.datas += [
    ("/absolute/path/to/some.txt","txt_files/some.txt","DATA"),
    ("/absolute/path/to/some2.txt","txt_files/some2.txt","DATA"),
    ("/absolute/path/to/some3.txt","txt_files/some3.txt","DATA"),
]

然后在您的程序中使用以下命令获取 .txt 文件的资源路径.

Then in your program use the following to get the resource path of your .txt files.

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.environ.get("_MEIPASS2",os.path.abspath("."))

    return os.path.join(base_path, relative_path)

 ...


 txt_data = open(resource_path("txt_files/some.txt")).read()

确保您像 python -m PyInstaller my_target.spec 一样构建它...在您编辑规范文件后,不要直接针对您的 .py 文件调用 PyInstaller,否则它会覆盖您编辑的文件...

Make sure you build it like python -m PyInstaller my_target.spec ... do not call PyInstaller directly against your .py file after you have edited your specification file or it will overwrite your edited file...

这篇关于PyInstaller 无法添加 .txt 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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