pyinstaller 好像没有找到数据文件 [英] pyinstaller seems not to find a data file

查看:47
本文介绍了pyinstaller 好像没有找到数据文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑 3:当我需要知道脚本/可执行文件的位置时,我将 __file__ 替换为 sys.argv[0].这并不完全相同,但在我的情况下它似乎运行良好(至少在可执行版本上......).现在一切正常,在单文件模式下,使用已接受答案的功能访问资源文件!

Edit 3: I replaced __file__ with sys.argv[0], when I need to know the location of my script/executable. This is not exactly the same, but in my case it seems to run fine (at least on executable version...). Now everything is working fine, in one-file mode, with use of accepted answer's function to access resource files!

编辑 2:如已接受答案的评论所示,问题来自我脚本中的路径解析;我尝试使用 __file__ 来获取脚本的位置,以便我可以访问它的资源文件.这在打包后不起作用,因为 __file__ 将从 Python.dll 返回文件名到脚本,所以总是没有路径,只有一个文件名.所以我必须找到另一个技巧来访问资源文件;目前的解决方法是将当前目录移动到可执行路径.

Edit 2: as shown in accepted answer's comments, problem is coming from path resolution in my script; I try to use __file__ to get the location of the script, so that I can access to its resource files. This does not work once packaged, as __file__ will return filename from Python.dll to the script, so quite always no path and just a file name. So I have to find another trick to make access to resource files; a work-around for the moment is to move current directory to the executable path.

顺便说一下,这意味着 ConfigParser 在访问文件时应该报告问题,而不是缺少某个部分.

By the way, this means that the ConfigParser should report problem when accessing the file, and not that a section is missing.

我会用我解决这个路径解析问题的方式来更新这个问题.

I'll update this question with the way I resolved this path resolution question.

我在使用 pyinstaller 时遇到问题,因为这是我第一次使用它,所以我肯定做错了什么.

I've got problems with pyinstaller, and as it's the first time I'm using it, it's sure that I've done something wrong.

所以,问题来了:pyisntaller 在我编写的脚本上运行流畅,并在 dist 文件夹中生成一些东西.好的,现在我想执行它,看看是否一切顺利,这是我得到的:

So, here's the problem: pyisntaller runs smoothly on a script I wrote, and generates some stuff in dist folder. Ok, so now I want to execute it to see if all went well, and here's what I get:

C:\Program Files\PyInstaller\pyinstaller-1.5.1>p_tool\dist\p_tool\p_tool.exe -?
Traceback (most recent call last):
  File "<string>", line 104, in <module>
  File "p_tool\build\pyi.win32\p_tool\outPYZ1.pyz/logging.config", line 76, in f
ileConfig
  File "p_tool\build\pyi.win32\p_tool\outPYZ1.pyz/logging.config", line 112, in
_create_formatters
  File "p_tool\build\pyi.win32\p_tool\outPYZ1.pyz/ConfigParser", line 532, in ge
t
ConfigParser.NoSectionError: No section: 'formatters'

我的第一个想法是 logging.conf 文件丢失了,所以我在 p_tool.spec 文件中添加了它(和其他一些资源文件),但是这个不是更好.

My first idea was that the logging.conf file was missing, so I added it (and some other resource files) in the p_tool.spec file, but this is not better.

Python 版本:2.6.6,WinXP 下.我正在使用 pyinstaller,因为我需要它来为 Solaris 工作站打包文件.

Python version: 2.6.6, under WinXP. I'm using pyinstaller as I will need it to package files for a Solaris workstation.

那么,有人遇到过这个问题吗?唯一相关的主题是以下问题:PyInstaller 问题,非常接近我的问题,但无可救药地没有答案.

So, anyone did have this problem? The only topic related is the following question: PyInstaller Problem, really close to my problem, but hopelessly it got no answer.

Edit3:删除了有关日志记录的详细信息,因为与问题无关.

details about logging removed, as not really related to the problem.

推荐答案

首先,在阅读之前打印 config_file/os.path.exists(config_file) 可能是明智的,这样你就可以确定文件在哪里如果python可以找到它.

Firstly, it might be wise to do a print config_file / os.path.exists(config_file) before reading it, so you can be sure where the file is and if python can find it.

至于实际访问它,os.path.split(__file__) 看起来几乎是正确的,但我不确定它在 pyinstaller 下是否正常工作 - 打包文件的正确方法是添加它们到 .spec 文件,pyinstaller 将在编译时加载它们并在运行时将它们解压到 $_MEIPASS2/.要在打包模式下获取 _MEIPASS2 目录并在解包(开发)模式下使用本地目录,我使用:

As to actually accessing it, os.path.split(__file__) looks almost correct, but I'm not sure it works properly under pyinstaller - the proper way of packing files is to add them to the .spec file, pyinstaller will then load them at compile time and unpack them to $_MEIPASS2/ at run time. To get the _MEIPASS2 dir in packed-mode and use the local directory in unpacked (development) mode, I use this:

def resource_path(relative):
    return os.path.join(
        os.environ.get(
            "_MEIPASS2",
            os.path.abspath(".")
        ),
        relative
    )


# in development
>>> resource_path("logging.conf")
"/home/shish/src/my_app/logging.conf"

# in deployment
>>> resource_path("logging.conf")
"/tmp/_MEI34121/logging.conf"

这篇关于pyinstaller 好像没有找到数据文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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