Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misleading error when opening file that does not exist #5489

Closed
naught101 opened this issue Jun 18, 2021 · 2 comments
Closed

Misleading error when opening file that does not exist #5489

naught101 opened this issue Jun 18, 2021 · 2 comments

Comments

@naught101
Copy link

What happened:

In [1]: import xarray as xr

In [2]: xr.__version__
Out[2]: '0.18.2'

In [3]: xr.open_dataset('/not-a-real-file')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-4cc5243e5a90> in <module>
----> 1 xr.open_dataset('/not-a-real-file')

~/miniconda3/envs/ana38/lib/python3.8/site-packages/xarray/backends/api.py in open_dataset(filename_or_obj, engine, chunks, cache, decode_cf, mask_and_scale, decode_times, decode_timedelta, use_cftime, concat_characters, decode_coords, drop_variables, backend_kwargs, *args, **kwargs)
    478 
    479     if engine is None:
--> 480         engine = plugins.guess_engine(filename_or_obj)
    481 
    482     backend = plugins.get_backend(engine)

~/miniconda3/envs/ana38/lib/python3.8/site-packages/xarray/backends/plugins.py in guess_engine(store_spec)
    109     installed = [k for k in engines if k != "store"]
    110     if installed:
--> 111         raise ValueError(
    112             "did not find a match in any of xarray's currently installed IO "
    113             f"backends {installed}. Consider explicitly selecting one of the "

ValueError: did not find a match in any of xarray's currently installed IO backends ['netcdf4', 'scipy']. Consider explicitly selecting one of the installed backends via the ``engine`` parameter to xarray.open_dataset(), or installing additional IO dependencies:
http://xarray.pydata.org/en/stable/getting-started-guide/installing.html
http://xarray.pydata.org/en/stable/user-guide/io.html

What you expected to happen:

Should produce a "FileNotFound" error first.

Engine hunting on a non-existent file is pointless, and the error message is pretty wordy, so my skim-reading originally misinterpreted it to think that for some reason my netcdf4 library wasn't installed, which lead me on to a 4-hour environment rebuild, with a sudden realisation that I'm an idiot at the end of it...

Possible solution:

assert(os.path.isfile(path)) before anything else.

@d70-t
Copy link
Contributor

d70-t commented Jun 18, 2021

I think there's more to think of then the suggested solution. For example when opening remote datasets (e.g. OPeNDAP resources), the supplied path will be a string which does not refer to a local path. The decision if a supplied "path" is valid might thus require to find an appropriate IO backend and then ask the backend if the supplied "path" is a valid one.

@keewis
Copy link
Collaborator

keewis commented Jun 18, 2021

See also #5329 for more discussion and #5351 for a proposed fix. I'm closing this as a duplicate, but feel free to continue in that issue / PR.

@keewis keewis closed this as completed Jun 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants