Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name 'Parser'

I have a bunch of modules. The modules and their imports are listed below:

ast.py:
    import enum
    from abc import ABC, abstractmethod
err.py:
    none
lexer.py:
    from token import TokenTag, Token
parser.py:
    from ast import *
    from err import UndeclaredIdentError, SyntaxError
    from token import TokenTag as Tag
    from type import Type
peep.py:
    from lexer import Lexer
    from parser import Parser
token.py:
    import enum
treewalker.py:
    from abc import ABC, abstractmethod
type.py:
    import enum
    from treewalker import TreeWalker

I tried to run peep.py but I get the following error:

Traceback (most recent call last):
  File "peep.py", line 2, in <module>
    from parser import Parser
ImportError: cannot import name 'Parser'

I don't understand why I got ImportError, I can't find any obvious circular dependencies in the file hierachy above. I did some research, I figured that I should rename the module ast.py to syntaxtree.py because ast.py already exists in Python's standard library. After renaming, it produced the same result. Any form of help is appreciated, thanks!

like image 281
Jimmy Yang Avatar asked Nov 07 '22 10:11

Jimmy Yang


1 Answers

Open parser.py file and change the code for from parser import Parser to from .parser import Parser

like image 82
Afianh Avatar answered Nov 15 '22 00:11

Afianh