Skip to content

Commit

Permalink
changed ValueError to ImportError (#5103)
Browse files Browse the repository at this point in the history
# changed ValueError to ImportError

Code cleaning.
Fixed inconsistencies in ImportError handling. Sometimes it raises
ImportError and sometime ValueError.
I've changed all cases to the `raise ImportError`
Also:
- added installation instruction in the error message, where it missed;
- fixed several installation instructions in the error message;
- fixed several error handling in regards to the ImportError
  • Loading branch information
leo-gan authored May 22, 2023
1 parent 5e47c64 commit c28cc0f
Show file tree
Hide file tree
Showing 51 changed files with 141 additions and 133 deletions.
2 changes: 1 addition & 1 deletion langchain/agents/agent_toolkits/pandas/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def create_pandas_dataframe_agent(
try:
import pandas as pd
except ImportError:
raise ValueError(
raise ImportError(
"pandas package not found, please install with `pip install pandas`"
)

Expand Down
2 changes: 1 addition & 1 deletion langchain/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def init_gptcache(cache_obj: gptcache.Cache, llm str):
try:
import gptcache # noqa: F401
except ImportError:
raise ValueError(
raise ImportError(
"Could not import gptcache python package. "
"Please install it with `pip install gptcache`."
)
Expand Down
2 changes: 1 addition & 1 deletion langchain/chains/moderation.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def validate_environment(cls, values: Dict) -> Dict:
openai.organization = openai_organization
values["client"] = openai.Moderation
except ImportError:
raise ValueError(
raise ImportError(
"Could not import openai python package. "
"Please install it with `pip install openai`."
)
Expand Down
2 changes: 1 addition & 1 deletion langchain/chat_models/azure_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def validate_environment(cls, values: Dict) -> Dict:
if openai_organization:
openai.organization = openai_organization
except ImportError:
raise ValueError(
raise ImportError(
"Could not import openai python package. "
"Please install it with `pip install openai`."
)
Expand Down
2 changes: 1 addition & 1 deletion langchain/document_loaders/apify_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def validate_environment(cls, values: Dict) -> Dict:

values["apify_client"] = ApifyClient()
except ImportError:
raise ValueError(
raise ImportError(
"Could not import apify-client Python package. "
"Please install it with `pip install apify-client`."
)
Expand Down
4 changes: 2 additions & 2 deletions langchain/document_loaders/docugami.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _parse_dgml(
try:
from lxml import etree
except ImportError:
raise ValueError(
raise ImportError(
"Could not import lxml python package. "
"Please install it with `pip install lxml`."
)
Expand Down Expand Up @@ -259,7 +259,7 @@ def _metadata_for_project(self, project: Dict) -> Dict:
try:
from lxml import etree
except ImportError:
raise ValueError(
raise ImportError(
"Could not import lxml python package. "
"Please install it with `pip install lxml`."
)
Expand Down
2 changes: 1 addition & 1 deletion langchain/document_loaders/duckdb_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def load(self) -> List[Document]:
try:
import duckdb
except ImportError:
raise ValueError(
raise ImportError(
"Could not import duckdb python package. "
"Please install it with `pip install duckdb`."
)
Expand Down
4 changes: 2 additions & 2 deletions langchain/document_loaders/image_captions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def load(self) -> List[Document]:
try:
from transformers import BlipForConditionalGeneration, BlipProcessor
except ImportError:
raise ValueError(
raise ImportError(
"`transformers` package not found, please install with "
"`pip install transformers`."
)
Expand All @@ -66,7 +66,7 @@ def _get_captions_and_metadata(
try:
from PIL import Image
except ImportError:
raise ValueError(
raise ImportError(
"`PIL` package not found, please install with `pip install pillow`"
)

Expand Down
2 changes: 1 addition & 1 deletion langchain/document_loaders/json_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(
try:
import jq # noqa:F401
except ImportError:
raise ValueError(
raise ImportError(
"jq package not found, please install it with `pip install jq`"
)

Expand Down
2 changes: 1 addition & 1 deletion langchain/document_loaders/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def load(
try:
import pandas as pd
except ImportError:
raise ValueError(
raise ImportError(
"pandas is needed for Notebook Loader, "
"please install with `pip install pandas`"
)
Expand Down
2 changes: 1 addition & 1 deletion langchain/document_loaders/onedrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def _auth(self) -> Type[Account]:
try:
from O365 import FileSystemTokenBackend
except ImportError:
raise ValueError(
raise ImportError(
"O365 package not found, please install it with `pip install o365`"
)
if self.auth_with_token:
Expand Down
10 changes: 5 additions & 5 deletions langchain/document_loaders/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def __init__(self, file_path: str) -> None:
try:
import pypdf # noqa:F401
except ImportError:
raise ValueError(
raise ImportError(
"pypdf package not found, please install it with " "`pip install pypdf`"
)
self.parser = PyPDFParser()
Expand Down Expand Up @@ -194,7 +194,7 @@ def __init__(self, file_path: str) -> None:
try:
from pdfminer.high_level import extract_text # noqa:F401
except ImportError:
raise ValueError(
raise ImportError(
"`pdfminer` package not found, please install it with "
"`pip install pdfminer.six`"
)
Expand Down Expand Up @@ -222,7 +222,7 @@ def __init__(self, file_path: str):
try:
from pdfminer.high_level import extract_text_to_fp # noqa:F401
except ImportError:
raise ValueError(
raise ImportError(
"`pdfminer` package not found, please install it with "
"`pip install pdfminer.six`"
)
Expand Down Expand Up @@ -256,7 +256,7 @@ def __init__(self, file_path: str) -> None:
try:
import fitz # noqa:F401
except ImportError:
raise ValueError(
raise ImportError(
"`PyMuPDF` package not found, please install it with "
"`pip install pymupdf`"
)
Expand Down Expand Up @@ -375,7 +375,7 @@ def __init__(
try:
import pdfplumber # noqa:F401
except ImportError:
raise ValueError(
raise ImportError(
"pdfplumber package not found, please install it with "
"`pip install pdfplumber`"
)
Expand Down
3 changes: 1 addition & 2 deletions langchain/document_loaders/readthedocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ def __init__(
"""Initialize path."""
try:
from bs4 import BeautifulSoup

except ImportError:
raise ValueError(
raise ImportError(
"Could not import python packages. "
"Please install it with `pip install beautifulsoup4`. "
)
Expand Down
2 changes: 1 addition & 1 deletion langchain/document_loaders/s3_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def load(self) -> List[Document]:
try:
import boto3
except ImportError:
raise ValueError(
raise ImportError(
"Could not import boto3 python package. "
"Please install it with `pip install boto3`."
)
Expand Down
2 changes: 1 addition & 1 deletion langchain/document_loaders/s3_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def load(self) -> List[Document]:
try:
import boto3
except ImportError:
raise ValueError(
raise ImportError(
"Could not import `boto3` python package. "
"Please install it with `pip install boto3`."
)
Expand Down
7 changes: 4 additions & 3 deletions langchain/document_loaders/sitemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(
try:
import lxml # noqa:F401
except ImportError:
raise ValueError(
raise ImportError(
"lxml package not found, please install it with " "`pip install lxml`"
)

Expand Down Expand Up @@ -107,8 +107,9 @@ def load(self) -> List[Document]:
try:
import bs4
except ImportError:
raise ValueError(
"bs4 package not found, please install it with " "`pip install bs4`"
raise ImportError(
"beautifulsoup4 package not found, please install it"
" with `pip install beautifulsoup4`"
)
fp = open(self.web_path)
soup = bs4.BeautifulSoup(fp, "xml")
Expand Down
4 changes: 2 additions & 2 deletions langchain/document_loaders/srt.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def __init__(self, file_path: str):
try:
import pysrt # noqa:F401
except ImportError:
raise ValueError(
"package `pysrt` not found, please install it with `pysrt`"
raise ImportError(
"package `pysrt` not found, please install it with `pip install pysrt`"
)
self.file_path = file_path

Expand Down
4 changes: 2 additions & 2 deletions langchain/document_loaders/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def load(self) -> List[Document]:
nest_asyncio.apply()
asyncio.run(self.fetch_data_from_telegram())
except ImportError:
raise ValueError(
raise ImportError(
"""`nest_asyncio` package not found.
please install with `pip install nest_asyncio`
"""
Expand All @@ -239,7 +239,7 @@ def load(self) -> List[Document]:
try:
import pandas as pd
except ImportError:
raise ValueError(
raise ImportError(
"""`pandas` package not found.
please install with `pip install pandas`
"""
Expand Down
2 changes: 1 addition & 1 deletion langchain/document_loaders/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def _dependable_tweepy_import() -> tweepy:
try:
import tweepy
except ImportError:
raise ValueError(
raise ImportError(
"tweepy package not found, please install it with `pip install tweepy`"
)
return tweepy
Expand Down
2 changes: 1 addition & 1 deletion langchain/document_loaders/url_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(
try:
import playwright # noqa:F401
except ImportError:
raise ValueError(
raise ImportError(
"playwright package not found, please install it with "
"`pip install playwright`"
)
Expand Down
4 changes: 2 additions & 2 deletions langchain/document_loaders/url_selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ def __init__(
try:
import selenium # noqa:F401
except ImportError:
raise ValueError(
raise ImportError(
"selenium package not found, please install it with "
"`pip install selenium`"
)

try:
import unstructured # noqa:F401
except ImportError:
raise ValueError(
raise ImportError(
"unstructured package not found, please install it with "
"`pip install unstructured`"
)
Expand Down
2 changes: 1 addition & 1 deletion langchain/embeddings/cohere.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def validate_environment(cls, values: Dict) -> Dict:

values["client"] = cohere.Client(cohere_api_key)
except ImportError:
raise ValueError(
raise ImportError(
"Could not import cohere python package. "
"Please install it with `pip install cohere`."
)
Expand Down
2 changes: 1 addition & 1 deletion langchain/embeddings/huggingface.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(self, **kwargs: Any):
import sentence_transformers

except ImportError as exc:
raise ValueError(
raise ImportError(
"Could not import sentence_transformers python package. "
"Please install it with `pip install sentence_transformers`."
) from exc
Expand Down
2 changes: 1 addition & 1 deletion langchain/embeddings/jina.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def validate_environment(cls, values: Dict) -> Dict:
try:
import jina
except ImportError:
raise ValueError(
raise ImportError(
"Could not import `jina` python package. "
"Please install it with `pip install jina`."
)
Expand Down
Loading

0 comments on commit c28cc0f

Please sign in to comment.