我正在尝试以 antlr 开始。当我导入模块antlr
时,它工作正常,但是如果我尝试导入 myGrammarlexer 和 myGrammarparser >,则表明MyGrammarlexer和Parser不在lib中。我使用pycharm,我安装了antlr: pip3 install antlr4-python3-runtime 我的代码是:
import sys
from antlr4 import *
import MyGrammarLexer
import MyGrammarParser
def main(argv):
input = FileStream(argv[1])
lexer = MyGrammarLexer(input)
stream = CommonTokenStream(lexer)
parser = MyGrammarParser(stream)
tree = parser.startRule()
if __name__ == '__main__':
main(sys.argv)
也许谁知道为什么我不能导入mygrammarlexer和mygrammarparser?请建议!接头:
/usr/bin/python3.6 /home/andrejka/PycharmProjects/Parser/parser.py
Traceback (most recent call last):
File "/home/andrejka/PycharmProjects/Parser/parser.py", line 2, in <module>
from antlr4 import *
File "/usr/lib/python3/dist-packages/antlr4/__init__.py", line 5, in <module>
from antlr4.BufferedTokenStream import TokenStream
File "/usr/lib/python3/dist-packages/antlr4/BufferedTokenStream.py", line 19, in <module>
from antlr4.error.Errors import IllegalStateException
File "/usr/lib/python3/dist-packages/antlr4/error/Errors.py", line 5, in <module>
from antlr4.atn.Transition import PredicateTransition
File "/usr/lib/python3/dist-packages/antlr4/atn/Transition.py", line 19, in <module>
from __builtin__ import unicode
ModuleNotFoundError: No module named '__builtin__'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Original exception was:
Traceback (most recent call last):
File "/home/andrejka/PycharmProjects/Parser/parser.py", line 2, in <module>
from antlr4 import *
File "/usr/lib/python3/dist-packages/antlr4/__init__.py", line 5, in <module>
from antlr4.BufferedTokenStream import TokenStream
File "/usr/lib/python3/dist-packages/antlr4/BufferedTokenStream.py", line 19, in <module>
from antlr4.error.Errors import IllegalStateException
File "/usr/lib/python3/dist-packages/antlr4/error/Errors.py", line 5, in <module>
from antlr4.atn.Transition import PredicateTransition
File "/usr/lib/python3/dist-packages/antlr4/atn/Transition.py", line 19, in <module>
from __builtin__ import unicode
ModuleNotFoundError: No module named '__builtin__'
我最近使用pddlpy python库遇到相同的错误时遇到了这个问题。我能够通过以下操作来解决它:
pip3 install antlr4-python3-runtime==4.9
最后的4.9版非常重要。
我发现包装的内容:
与Pip3安装的软件包不同。
我替换了:
的内容~/anaconda3/envs/py3/lib/python3.6/site-packages/antlr4
使用我手动下载和提取的包装中的内容:
~/Downloads/antlr4-python3-runtime-4.7/src/antlr4
它似乎现在正在工作 - 特别是以下错误:
ModuleNotFoundError: No module named '__builtin__'
没有发生。
发布您的追溯后,我可以看到错误是: ModuleNotFoundError: No module named '__builtin__'
这意味着您尝试导入的软件包使用Python2语法,但是您正在使用Python3。
如果您需要运行此软件包,则应使用Python2,但请记住,如果这是一个新项目,您可能想搜索其他项目。
更多关于为什么它在这里不工作的更多信息
编辑他们发布了一个带有Python3支持的软件包,但似乎您安装/使用了不同的东西。您应该验证它。