Solving AttributeError in tiktoken: Circular Import Issue
The article describe how to solve AttributeError in when using toktoken package.
Today, I decided to explore some code related to large language models (LLMs). I created a Python file named token.py
to test things out. However, when I ran the script, I encountered the following error:
AttributeError: partially initialized module 'tiktoken' has no attribute 'encoding_for_model' (most likely due to a circular import)
Initially, I suspected that the issue might be related to the package installation. To confirm this, I ran the following command to check all installed packages:
pip list
After reviewing the output, I realized that I already had the latest version of the package installed. So, I tried running the example script from the tiktoken
documentation to see if it worked:
import tiktoken
enc = tiktoken.get_encoding("cl100k_base")
assert enc.decode(enc.encode("hello world")) == "hello world"
# To get the tokenizer corresponding to a specific model in the OpenAI API:
enc = tiktoken.encoding_for_model("gpt-4")
Unfortunately, I received the same error:
AttributeError: partially initialized module 'tiktoken' has no attribute 'get_encoding' (most likely due to a circular import)
The Solution
After investigating the issue, I discovered the root cause: I had named my script token.py
. This caused a circular import conflict with the tiktoken
module. Renaming the file to llm_token.py
resolved the problem, and the script ran successfully.