Find answers from the community

Updated 4 months ago

Import error

At a glance

A community member updated LlamaIndex and encountered an error: "ImportError: cannot import name 'PromptTemplate' from 'llama_index.prompts'". The community members suggested trying to uninstall and reinstall llama-index, but the issue persisted. After reinstalling, the error occurred when trying to import PromptTemplate or call index.as_query_engine(). The community members provided the full traceback and noted that the source code looked fine. Eventually, the issue was resolved by downgrading and upgrading the package, though the exact cause was unclear.

Useful resources
I just updated LlamaIndex and got this error: ImportError: cannot import name 'PromptTemplate' from 'llama_index.prompts' (/Users/jana/work/LlamaIndexLangChain/jupyter/myenv/lib/python3.9/site-packages/llama_index/prompts/init.py) upon calling this query_engine = index.as_query_engine() on a vector index. Anyone experiencing the same issue and know how to resolve it?
L
J
7 comments
Hmm, maybe try uninstalling and reinstalling llama-index?

Do you have the full traceback?

The source code looks fine to me. It also works for me locally πŸ‘€ https://github.com/jerryjliu/llama_index/blob/main/llama_index/prompts/__init__.py
I have reinstalled the llama-index. Error occurs when I run from llama_index.prompts import PromptTemplate trace is simple: ---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Cell In[18], line 1
----> 1 from llama_index.prompts import PromptTemplate

ImportError: cannot import name 'PromptTemplate' from 'llama_index.prompts' (/Users/jana/work/LlamaIndexLangChain/jupyter/myenv/lib/python3.9/site-packages/llama_index/prompts/init.py)
If I run : query_engine = index.as_query_engine() then the trace is ---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Cell In[19], line 1
----> 1 query_engine = index.as_query_engine()

File ~/work/LlamaIndexLangChain/jupyter/myenv/lib/python3.9/site-packages/llama_index/indices/base.py:335, in BaseIndex.as_query_engine(self, kwargs) 333 def as_query_engine(self, kwargs: Any) -> BaseQueryEngine:
334 # NOTE: lazy import
--> 335 from llama_index.query_engine.retriever_query_engine import RetrieverQueryEngine
337 retriever = self.as_retriever(**kwargs)
339 kwargs["retriever"] = retriever

File ~/work/LlamaIndexLangChain/jupyter/myenv/lib/python3.9/site-packages/llama_index/query_engine/init.py:1
----> 1 from llama_index.query_engine.citation_query_engine import CitationQueryEngine
2 from llama_index.query_engine.flare.base import FLAREInstructQueryEngine
3 from llama_index.query_engine.graph_query_engine import ComposableGraphQueryEngine

File ~/work/LlamaIndexLangChain/jupyter/myenv/lib/python3.9/site-packages/llama_index/query_engine/citation_query_engine.py:10
8 from llama_index.indices.query.base import BaseQueryEngine
9 from llama_index.indices.query.schema import QueryBundle
---> 10 from llama_index.prompts import PromptTemplate
11 from llama_index.prompts.base import BasePromptTemplate
12 from llama_index.response.schema import RESPONSE_TYPE

ImportError: cannot import name 'PromptTemplate' from 'llama_index.prompts' (/Users/jana/work/LlamaIndexLangChain/jupyter/myenv/lib/python3.9/site-packages/llama_index/prompts/init.py)
is there anything else I could do ?
I downgraded and upgraded and somehow it started working... one of those strange things
So, it's ok, thank you!
Nice! Must have been a cached file or something πŸ˜…
Add a reply
Sign up and join the conversation on Discord