Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python cannot import 'Token' from 'token'

Tags:

python

import

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
like image 995
an inconspicuous semicolon Avatar asked Jul 22 '26 03:07

an inconspicuous semicolon


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

like image 169
Sadaf Shafi Avatar answered Jul 24 '26 16:07

Sadaf Shafi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!