无法导入模块ANTLR MyGrammarLexer和MyGrammarParser

时间:2017-09-06 15:35:04

标签: python python-3.x pycharm antlr antlr4

我试图从 ANTLR 开始。当我导入模块antlr时,它工作正常,但如果我尝试导入 MyGrammarLexer MyGrammarParser ,它会显示MyGrammarLexer和解析器不在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__'

3 个答案:

答案 0 :(得分:2)

我发现了包裹的内容:

https://pypi.python.org/packages/0b/6b/30c5b84d203b62e1412d14622e3bae6273399d79d20f3a24c8145213f610/antlr4-python3-runtime-4.7.tar.gz#md5=190245a0fb4abf43568489a4b6e33aba

与pip3安装的软件包不同。

我已取代以下内容:

~/anaconda3/envs/py3/lib/python3.6/site-packages/antlr4

包含我已下载并手动提取的包中的内容:

~/Downloads/antlr4-python3-runtime-4.7/src/antlr4

现在似乎正在运作 - 尤其是以下错误:

ModuleNotFoundError: No module named '__builtin__'

没有发生。

答案 1 :(得分:0)

发布回溯后我可以看到错误是: ModuleNotFoundError: No module named '__builtin__'

这意味着您尝试导入的包使用python2语法,但您使用的是python3。

如果你需要运行这个软件包,你应该使用python2,但请记住,如果这是一个新项目,你可能想要搜索另一个软件包。

更多关于它无效的原因here

修改 他们发布了package支持python3,但似乎你安装/使用了不同的东西。你应该验证它。

答案 2 :(得分:0)

最近在使用pddlpy python库遇到相同错误时遇到了这个问题。我可以通过以下操作对其进行修复:

pip3 install antlr4-python3-runtime==4.9

最后的4.9版本非常重要。