Can't import module ANTLR MyGrammarLexer and MyGrammarParser

雨燕双飞 提交于 2019-12-11 05:45:49

问题


I'm trying to start with ANTLR . When I import module antlr it's working just fine , but if I try to import MyGrammarLexer and MyGrammarParser , it's shows that MyGrammarLexer and Parser aren't in lib. I Using PyCharm , I installed ANTLR with : pip3 install antlr4-python3-runtime my code is :

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)

Maybe whom know why i can't import MyGrammarLexer and MyGrammarParser? Please suggest! Tackback:

    /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__'

回答1:


I found out that content of the package:

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

is different from the package installed by pip3.

I have replaced content of:

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

with content from the package I have downloaded and extracted manually:

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

It seems to be working now - in particular the following error:

ModuleNotFoundError: No module named '__builtin__'

does not occur.




回答2:


After posting your traceback I can see the error is: ModuleNotFoundError: No module named '__builtin__'

That means the package you try to import is using python2 syntax, but you're using python3.

You should use python2 if you need to run this package, but keep in mind that you probably want to search for a different one if this is a new project.

More about why it's not working here

EDIT They released a package with python3 support, but it seems you installed/used something different. You should verify it.



来源:https://stackoverflow.com/questions/46079258/cant-import-module-antlr-mygrammarlexer-and-mygrammarparser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!