pola-rs/polars · error · ImportError

`fsspec` is required for `storage_options` argument

Error message

`fsspec` is required for `storage_options` argument

What it means

Polars raises this ImportError in prepare_file_arg when a caller passes `storage_options` but the optional `fsspec` package is not installed. storage_options are only meaningful for fsspec-handled sources (remote URLs, cloud filesystems), so without fsspec there is no way to apply them.

Solutions

  1. Install fsspec: pip install fsspec (or pip install 'polars[fsspec]').
  2. Install the filesystem-specific companion (e.g. s3fs, gcsfs, adlfs) alongside fsspec.
  3. If the source is local and storage_options were passed by mistake, remove the storage_options argument.

Example fix

// before
 df = pl.read_csv("s3://bucket/file.csv", storage_options={"key": k, "secret": s})
// after
 # pip install fsspec s3fs
 df = pl.read_csv("s3://bucket/file.csv", storage_options={"key": k, "secret": s})
Defensive patterns

Strategy: validation

Validate before calling

import importlib.util
if storage_options and importlib.util.find_spec('fsspec') is None:
    raise RuntimeError('fsspec is required when passing storage_options')

Type guard

def fsspec_available() -> bool:
    return importlib.util.find_spec('fsspec') is not None

Try / catch

try:
    df = pl.read_csv(url, storage_options=opts)
except ImportError as e:
    if 'fsspec' in str(e):
        raise SystemExit("Install fsspec: pip install 'polars[fsspec]'") from e
    raise

Prevention

When it happens

Trigger: Calling pl.read_csv/read_parquet/etc. (or scan_ variants that route through prepare_file_arg) with storage_options={...} set while fsspec is absent from the environment.

Common situations: Reading from S3/GCS/Azure/HTTP in a fresh environment; CI containers with a slim polars install; code copied from a notebook that had fsspec installed; a dependency bump that dropped the extra `polars[fsspec]`.

Understand the failure class

Background: "X is not installed. Please install it with pip install Y": missing optional dependency errors — ImportError/ValueError raised when a library's optional extra was never installed — this error's family across 22 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/a756e1abe66a419d. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/io/_utils.py:169

    An http URL is read into a buffer and returned as a `BytesIO`.

    When `encoding` is not `utf8` or `utf8-lossy`, the whole file is
    first read in Python and decoded using the specified encoding and
    returned as a `BytesIO` (for usage with `read_csv`). If encoding
    ends with "-lossy", characters that can't be decoded are replaced
    with `�`.

    A `bytes` file is returned as a `BytesIO` if `use_pyarrow=True`.

    When fsspec is installed, remote file(s) is (are) opened with
    `fsspec.open(file, **kwargs)` or `fsspec.open_files(file, **kwargs)`.
    If encoding is not `utf8` or `utf8-lossy`, decoding is handled by
    fsspec too.
    """
    storage_options = storage_options.copy() if storage_options else {}
    if storage_options and not _FSSPEC_AVAILABLE:
        msg = "`fsspec` is required for `storage_options` argument"
        raise ImportError(msg)

    # Small helper to use a variable as context
    @contextmanager
    def managed_file(file: Any) -> Iterator[Any]:
        try:
            yield file
        finally:
            pass

    has_utf8_utf8_lossy_encoding = (
        encoding in {"utf8", "utf8-lossy"} if encoding else True
    )
    encoding_str = encoding if encoding else "utf8"
    encoding_str, encoding_errors = (
        (encoding_str[:-6], "replace")
        if encoding_str.endswith("-lossy")
        else (encoding_str, "strict")
    )

View on GitHub (pinned to fe841f959e)