【问题标题】:Can't get lexers.PythonLexer() from Kivy app built with PyInstaller无法从使用 PyInstaller 构建的 Kivy 应用程序中获取 lexers.PythonLexer()
【发布时间】:2014-07-12 03:38:35
【问题描述】:

我有一个运行良好的 Python 应用程序,我在其中执行以下操作:

from pygments import lexers

那么……

testing = lexers.PythonLexer()

只需运行脚本就可以了,我得到了 PythonLexer 的一个新实例。但是...当我使用 PyInstaller 为应用程序创建一个构建文件夹然后运行它时,该行失败:

文件“blah\myfile”,第 31 行,在 __init__ 中 AttributeError:“模块”对象没有属性“PythonLexer”

有什么想法吗?我认为这是因为 pygments 在运行时以某种方式从我的 PyInstaller 构建文件夹中缺少的一些文件构建其对象,但我不太明白如何。

该应用正在使用 Kivy,但实际上我认为这与此问题无关。

【问题讨论】:

    标签: python kivy pyinstaller pygments


    【解决方案1】:

    问题是包pygments.lexers 不包含名为PythonLexer.py 的文件。要解决问题,您可以这样做:

    from pygments.lexers.agile import PythonLexer
    testing = PythonLexer()
    

    【讨论】:

    • 这确实解决了这个问题。但是,我需要使用 PyInstaller 设置而不是通过编辑我的文件来解决这个问题(有问题的文件实际上不是我的,它在 Kivy 内部)。我希望 hook-pygments.lexers.py 文件涵盖了这一点,但默认的钩子文件已经包含 hiddenimports = ['agile', 'dotnet'] 所以我不明白为什么它不执行导入.
    • 只想补充,我也遇到这个问题,无法解决。最终我摆脱了使用 rstwidget 和 codeinput 的 *.kv,并修改 main.py 以不显示源代码。
    • ... 再次在这里,回到自己的评论... :( 知道为什么 Kivy 应用程序找不到 PythonLexer 吗?我已经安装了这些:“sudo pip install pyglet docutils”
    【解决方案2】:

    您可以通过使用 PyInstaller 的一项建议来修复它,以包含未自动找到的模块。 http://pythonhosted.org/PyInstaller/#helping-pyinstaller-find-modules

    【讨论】:

      【解决方案3】:

      我在使用pyinstaller打包Kivy的'showcase'演示应用时遇到了类似的问题。

      这似乎是一个 Pygments 错误。

      我打了\pygments\lexers\__init__.py之后,错误就消失了:

      --- __init__old.py
      +++ __init__.py
      @@ -15,6 +15,7 @@
      import fnmatch
      from os.path import basename
      
      +from pygments.lexers.agile import PythonLexer
      from pygments.lexers._mapping import LEXERS
      from pygments.modeline import get_filetype_from_buffer
      

      【讨论】:

        猜你喜欢
        • 2015-11-22
        • 2022-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多