在 ply 中使用 py2exe 打包 python 程序出现奇怪的错误?

Using py2exe packing python program with ply got strange error?

我下载了 PLY,运行 在 ply3.8/test/calclex.py

中进行了简单测试
# -----------------------------------------------------------------------------
# calclex.py
# -----------------------------------------------------------------------------
import sys

if ".." not in sys.path: sys.path.insert(0,"..")
import ply.lex as lex

tokens = (
    'NAME','NUMBER',
    'PLUS','MINUS','TIMES','DIVIDE','EQUALS',
    'LPAREN','RPAREN',
    )

# Tokens

t_PLUS    = r'\+'
t_MINUS   = r'-'
t_TIMES   = r'\*'
t_DIVIDE  = r'/'
t_EQUALS  = r'='
t_LPAREN  = r'\('
t_RPAREN  = r'\)'
t_NAME    = r'[a-zA-Z_][a-zA-Z0-9_]*'

def t_NUMBER(t):
    r'\d+'
    try:
        t.value = int(t.value)
    except ValueError:
        print("Integer value too large %s" % t.value)
        t.value = 0
    return t

t_ignore = " \t"

def t_newline(t):
    r'\n+'
    t.lexer.lineno += t.value.count("\n")

def t_error(t):
    print("Illegal character '%s'" % t.value[0])
    t.lexer.skip(1)

# Build the lexer
lexer = lex.lex()

效果很好。但是当我使用 py2exe 将它打包成可执行文件时。当 运行 它时,我收到如下错误:

Traceback (most recent call last):
  File "calclex.py", line 46, in <module>
    lexer = lex.lex()
  File "ply\lex.pyc", line 906, in lex
  File "ply\lex.pyc", line 580, in validate_all
  File "ply\lex.pyc", line 822, in validate_rules
  File "ply\lex.pyc", line 833, in validate_module
  File "inspect.pyc", line 690, in getsourcelines
  File "inspect.pyc", line 526, in findsource
  File "inspect.pyc", line 403, in getfile
TypeError: <module '__main__' (built-in)> is a built-in module

有没有人试过将 ply 打包成可执行文件?
而我的setup.py如下:

from distutils.core import setup
import py2exe
setup(console=["calclex.py"])

Ply 坚持其语法在真实文件中定义,而不是虚拟化文件系统。所以它不能与 py2exe 或 pyinstaller 或其他试图将 python 源文件打包到单个档案中的此类程序员一起使用。 (另请参阅 。)

我不知道有什么简单的解决方法。也许应该将其作为功能请求报告给 Ply 维护者。