Python+ANTLR4: No module named antlr4

两盒软妹~` 提交于 2019-12-13 12:34:55

问题


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.


回答1:


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



回答2:


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!



来源:https://stackoverflow.com/questions/42737716/pythonantlr4-no-module-named-antlr4

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