| 15 | |
| 16 | |
| 17 | class LangroidImportError(ImportError): |
| 18 | def __init__( |
| 19 | self, |
| 20 | package: Optional[str] = None, |
| 21 | extra: Optional[str | List[str]] = None, |
| 22 | error: str = "", |
| 23 | *args: object, |
| 24 | ) -> None: |
| 25 | """ |
| 26 | Generate helpful warning when attempting to import package or module. |
| 27 | |
| 28 | Args: |
| 29 | package (str): The name of the package to import. |
| 30 | extra (str): The name of the extras package required for this import. |
| 31 | error (str): The error message to display. Depending on context, we |
| 32 | can set this by capturing the ImportError message. |
| 33 | |
| 34 | """ |
| 35 | if error == "" and package is not None: |
| 36 | error = f"{package} is not installed by default with Langroid.\n" |
| 37 | |
| 38 | if extra: |
| 39 | if isinstance(extra, list): |
| 40 | help_preamble = f""" |
| 41 | If you want to use it, please install langroid with one of these |
| 42 | extras: {', '.join(extra)}. The examples below use the first one, |
| 43 | i.e. {extra[0]}. |
| 44 | """ |
| 45 | extra = extra[0] |
| 46 | else: |
| 47 | help_preamble = f""" |
| 48 | If you want to use it, please install langroid with the |
| 49 | `{extra}` extra. |
| 50 | """ |
| 51 | |
| 52 | install_help = f""" |
| 53 | {help_preamble} |
| 54 | |
| 55 | If you are using pip: |
| 56 | pip install "langroid[{extra}]" |
| 57 | |
| 58 | For multiple extras, you can separate them with commas: |
| 59 | pip install "langroid[{extra},another-extra]" |
| 60 | |
| 61 | If you are using Poetry: |
| 62 | poetry add langroid --extras "{extra}" |
| 63 | |
| 64 | For multiple extras with Poetry, list them with spaces: |
| 65 | poetry add langroid --extras "{extra} another-extra" |
| 66 | |
| 67 | If you are using uv: |
| 68 | uv add "langroid[{extra}]" |
| 69 | |
| 70 | For multiple extras with uv, you can separate them with commas: |
| 71 | uv add "langroid[{extra},another-extra]" |
| 72 | |
| 73 | If you are working within the langroid dev env (which uses uv), |
| 74 | you can do: |
no outgoing calls
no test coverage detected
searching dependent graphs…