6

I'm trying to make a lexer in python but when I try to import a class from file token.py I get this error

ImportError: cannot import name 'Token' from 'token'

the code for token.py is as follows

from enum import Enum


class Token():
    def __init__(self, ttype, value=None):
        self.type = ttype
        self.value = value

    def __repr__(self):
        return {'type':self.type, 'value':self.value}
    


class TokenType(Enum):
    NUMBER = 0
    PLUS = 1
    MINUS = 2
    MULTIPLY = 3
    DIVIDE = 4
    LPAREN = 5
    RPAREN = 6

and the import statement is

from token import Token, TokenType
0

1 Answer 1

11

There is a library in python called token, so your interpreter might be confusing it with the inbuilt python library. Try to rename the library. Name it token_2.py or something

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.