我下载了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"])
答案 0 :(得分:0)
Ply坚持认为其语法是在真实文件中定义的,而不是在虚拟化文件系统中定义的。因此,它不能使用py2exe或pyinstaller或其他试图将python源文件打包到单个档案中的程序员。 (另见Pyinstaller and Ply IOError: source code not available。)
我不知道一个简单的解决方法。也许它应该作为对Ply维护者的功能请求报告。