4

I have a Python app which works very nicely, inside of which I do something like:

from pygments import lexers

then...

testing = lexers.PythonLexer()

Just running the script works great and I get a new instance of the PythonLexer. However... when I make a build folder for the app using PyInstaller and then run it, that line fails:

File "blah\myfile", line 31, in __init__ AttributeError: 'module' object has no attribute 'PythonLexer'

Any ideas? I think it's because pygments is somehow building up its objects at runtime from some files that are missing in my PyInstaller build folder, but I can't quite see how.

The app is using Kivy, but I actually don't think that's too much to do with this issue.

Chris Rae
  • 5,627
  • 2
  • 36
  • 51

3 Answers3

2

The problem is that the package pygments.lexers did not contain the file named PythonLexer.py. To resolve problem, you can do so:

from pygments.lexers.agile import PythonLexer
testing = PythonLexer()
NorthCat
  • 9,643
  • 16
  • 47
  • 50
  • This does indeed fix the issue. However, I need to fix this using the PyInstaller setup rather than by editing my file (the file in question is not actually mine, it's inside Kivy). I'd expect this to be covered by the hook-pygments.lexers.py file but the default hook file already contains hiddenimports = ['agile', 'dotnet'] so I can't see why it doesn't do the import. – Chris Rae May 23 '14 at 21:58
  • Just want to add, I also come across this issue and unable to solve this. Eventually I get rid of *.kv that use rstwidget and codeinput, and modify `main.py` to not show source code. – swdev Sep 20 '14 at 22:58
  • ... here me again, getting back to own comment.... :( Any idea why a Kivy app can't find PythonLexer? I have install these: "sudo pip install pyglet docutils" – swdev Nov 15 '14 at 02:50
1

You may be able to fix it by using one of PyInstaller's suggestions for including modules that weren't found automatically. http://pythonhosted.org/PyInstaller/#helping-pyinstaller-find-modules

brousch
  • 1,064
  • 5
  • 10
0

I encountered a similar problem when using pyinstaller to package Kivy's 'showcase' demo app.

It seems to be a Pygments bug.

After I patched \pygments\lexers\__init__.py , the error is gone:

--- __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
micmejia
  • 11
  • 2