5

I would like to use ANTLR4 with Python 2.7 and for this I did the following:

  1. I installed the package antlr4-4.6-1 on Arch Linux with sudo pacman -S antlr4.

  2. I wrote a MyGrammar.g4 file and successfully generated Lexer and Parser Code with antlr4 -Dlanguage=Python2 MyGrammar.g4

  3. Now executing for example the generated Lexer code with python2 MyGrammarLexer.py results in the error ImportError: No module named antlr4.

What could to be the problem? FYI: I have both Python2 and Python3 installed - I don't know if that might cause any trouble.

RogerS
  • 1,322
  • 9
  • 11
ec-m
  • 779
  • 1
  • 5
  • 15

3 Answers3

10

You need to install antlr4 for python through pip:

sudo pip install antlr4-python2-runtime

If you don't have pip on you system, install it first with:

sudo pacman -S python2-pip
devopsfun
  • 1,368
  • 2
  • 15
  • 37
  • 1
    I tried this but sadly it did not work. After executing the pip install command it says `Requirement already satisfied: antlr4-python2-runtime in /usr/lib/python3.6/site-packages`. It might be the case that pip does not work correctly for the two python versions but I don't know how to figure out the problem... – ec-m Mar 12 '17 at 17:33
  • I just copied the antlr4 files from `/usr/lib/python3.6/site-packages/` to `/usr/lib/python2.7/site-packages/` and this solved the problem! – ec-m Mar 12 '17 at 17:41
  • @ec-m Maybe you could try `sudo pip2 install antlr4-python2-runtime`, because this would call `pip` for Python2. – devopsfun Mar 12 '17 at 18:25
  • Oh, perfect! I didn't know that this is possible. Thank you :) – ec-m Mar 14 '17 at 11:04
1

The problem was that antlr4 was only installed for Python3 and not Python2. I simply copied the antlr4 files from /usr/lib/python3.6/site-packages/ to /usr/lib/python2.7/site-packages/ and this solved the problem!

ec-m
  • 779
  • 1
  • 5
  • 15
0

try:

pip install antlr4-python2-runtime

or:

python -m pip install antlr4-python2-runtime