Python cannot import 'Token' from 'token'
Asked Answered
K

2

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
Knobloch answered 17/10, 2020 at 11:27 Comment(0)
W
12

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

Wigwam answered 17/10, 2020 at 11:45 Comment(0)
L
0

don't forget to save all files with dependencies in your code before launching the application. My code working after saving all files.

Lander answered 12/1 at 23:28 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.