When antlr4 generates python3 lexer and parser files, it uses an implicit relative import of the parser from the lexer file:
# RLangLexer.py
...
from RLangParser import RLangParser
...
Apparently, this kind of import is a no-go for Python3, and at least pytest breaks when a test uses an imported file which itself tries to make an implicit relative import. The solution is simple, add a period to make it an explicit relative import:
# RLangLexer.py
...
from .RLangParser import RLangParser
...
This makes running pytest possible (and I presume running a listener which uses the lexer from outside the package). However, it breaks pygrun with an ImportError:
...
ImportError: attempted relative import with no known parent package
No fear, a simple fix to thepygrun file which updates the __package__ global with the current working directory allows for pygrun to work with both implicit and explicit relative imports:
# pygrun
102 sys.path.append('.')
103 #print(sys.path)
104 p = os.path.basename(os.getcwd()) # New Code
105 globals().update({'__package__': p}) # New Code
106
107 # print("Load Lexer {}".format(lexerName))
108 module_lexer = __import__(lexerName, globals(), locals(), lexerName, 1)
109 class_lexer = getattr(module_lexer, lexerName)
I can add the changes to pygrun to support these imports with a pull request, but I'm not sure how to change the initial generation of implicit relative imports in the lexer (which would add a . to the beginning of the parser module) without a post-processing script which adds a .. That would be quite messy. Is there an internal fix?
When antlr4 generates python3 lexer and parser files, it uses an implicit relative import of the parser from the lexer file:
Apparently, this kind of import is a no-go for Python3, and at least
pytestbreaks when a test uses an imported file which itself tries to make an implicit relative import. The solution is simple, add a period to make it an explicit relative import:This makes running
pytestpossible (and I presume running a listener which uses the lexer from outside the package). However, it breakspygrunwith anImportError:No fear, a simple fix to the
pygrunfile which updates the__package__global with the current working directory allows forpygrunto work with both implicit and explicit relative imports:I can add the changes to
pygrunto support these imports with a pull request, but I'm not sure how to change the initial generation of implicit relative imports in the lexer (which would add a.to the beginning of the parser module) without a post-processing script which adds a.. That would be quite messy. Is there an internal fix?