2

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__'
Or Duan
  • 13,142
  • 6
  • 60
  • 65
Андрей Ка
  • 756
  • 4
  • 14
  • 33

3 Answers3

5

I ran into this problem recently when getting the same error using the pddlpy python library. I was able to fix it by doing the following:

pip3 install antlr4-python3-runtime==4.9

The version 4.9 at the end is very important.

Dustin
  • 143
  • 2
  • 12
2

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.

fairf4x
  • 21
  • 2
1

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.

Or Duan
  • 13,142
  • 6
  • 60
  • 65