Python cannot import 'Token' from 'token'

Viewed 4780

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
1 Answers

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

Related