pythonparsingimportname-collision

Python 3 import conflicts with internal "parser"


.
├── gen.py
├── lexer
│   ├── engine.py
|   └── ...
└── parser
    ├── engine.py
    └── ...

I'm writing my compiler project, but now i'm stuck with python import conflicts. In gen.py, I want to import some function for code generation, like

import lexer.engine   # OK
import parser.engine  # ModuleNotFoundError: No module named 'parser.engine'; 'parser' is not a package

After some investigation, I've learned that "parser" is reserved for python internal parser. But I cannot change the directory name "parser", since it has been used everywhere.

How can I solve the problem?


Solution

  • To be accepted as a Python package, a directory must have an __init__.py file.
    Variables declared in this file can be access as if the package is a regular module. It can also be empty.

    tl;dr: Add an empty __init__.py file to the directory and it should work.