Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: load() missing 1 required positional argument: 'Loader' in Google Colab

I am trying to do a regular import in Google Colab.
This import worked up until now.
If I try:

import plotly.express as px

or

import pingouin as pg

I get an error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-86e89bd44552> in <module>()
----> 1 import plotly.express as px

9 frames
/usr/local/lib/python3.7/dist-packages/plotly/express/__init__.py in <module>()
     13     )
     14 
---> 15 from ._imshow import imshow
     16 from ._chart_types import (  # noqa: F401
     17     scatter,

/usr/local/lib/python3.7/dist-packages/plotly/express/_imshow.py in <module>()
      9 
     10 try:
---> 11     import xarray
     12 
     13     xarray_imported = True

/usr/local/lib/python3.7/dist-packages/xarray/__init__.py in <module>()
      1 import pkg_resources
      2 
----> 3 from . import testing, tutorial, ufuncs
      4 from .backends.api import (
      5     load_dataarray,

/usr/local/lib/python3.7/dist-packages/xarray/tutorial.py in <module>()
     11 import numpy as np
     12 
---> 13 from .backends.api import open_dataset as _open_dataset
     14 from .backends.rasterio_ import open_rasterio as _open_rasterio
     15 from .core.dataarray import DataArray

/usr/local/lib/python3.7/dist-packages/xarray/backends/__init__.py in <module>()
      4 formats. They should not be used directly, but rather through Dataset objects.
      5 
----> 6 from .cfgrib_ import CfGribDataStore
      7 from .common import AbstractDataStore, BackendArray, BackendEntrypoint
      8 from .file_manager import CachingFileManager, DummyFileManager, FileManager

/usr/local/lib/python3.7/dist-packages/xarray/backends/cfgrib_.py in <module>()
     14     _normalize_path,
     15 )
---> 16 from .locks import SerializableLock, ensure_lock
     17 from .store import StoreBackendEntrypoint
     18 

/usr/local/lib/python3.7/dist-packages/xarray/backends/locks.py in <module>()
     11 
     12 try:
---> 13     from dask.distributed import Lock as DistributedLock
     14 except ImportError:
     15     DistributedLock = None

/usr/local/lib/python3.7/dist-packages/dask/distributed.py in <module>()
      1 # flake8: noqa
      2 try:
----> 3     from distributed import *
      4 except ImportError:
      5     msg = (

/usr/local/lib/python3.7/dist-packages/distributed/__init__.py in <module>()
      1 from __future__ import print_function, division, absolute_import
      2 
----> 3 from . import config
      4 from dask.config import config
      5 from .actor import Actor, ActorFuture

/usr/local/lib/python3.7/dist-packages/distributed/config.py in <module>()
     18 
     19 with open(fn) as f:
---> 20     defaults = yaml.load(f)
     21 
     22 dask.config.update_defaults(defaults)

TypeError: load() missing 1 required positional argument: 'Loader'

I think it might be a problem with Google Colab or some basic utility package that has been updated, but I can not find a way to solve it.

like image 485
user17147261 Avatar asked Oct 14 '21 03:10

user17147261


4 Answers

Found the problem.
I was installing pandas_profiling, and this package updated pyyaml to version 6.0 which is not compatible with the current way Google Colab imports packages.
So just reverting back to pyyaml version 5.4.1 solved the problem.

For more information check versions of pyyaml here.
See this issue and formal answers in GitHub

##################################################################
For reverting back to pyyaml version 5.4.1 in your code, add the next line at the end of your packages installations:

!pip install pyyaml==5.4.1

It is important to put it at the end of the installation, some of the installations will change the pyyaml version.

like image 115
user17147261 Avatar answered Oct 23 '22 14:10

user17147261


Now, the load() function requires parameter loader=Loader.

If your YAML file contains just simple YAML (str, int, lists), try to use yaml.safe_load() instead of yaml.load(). And If you need FullLoader, you can use yaml.full_load().

Starting from pyyaml>=5.4, it doesn't have any discovered critical vulnerabilities, pyyaml status.

source: https://stackoverflow.com/a/1774043/13755823

yaml.safe_load() should always be preferred unless you explicitly need the arbitrary object serialization/deserialization provided in order to avoid introducing the possibility for arbitrary code execution.

More about yaml.load(input) here.

like image 33
Bohdan Pylypenko Avatar answered Oct 23 '22 12:10

Bohdan Pylypenko


this worked for me

config = yaml.load(ymlfile, Loader=yaml.Loader)
like image 5
Ali Ait-Bachir Avatar answered Oct 23 '22 13:10

Ali Ait-Bachir


The Python "TypeError: load() missing 1 required positional argument: 'Loader'" occurs when we use the yaml.load() method without specifying the Loader keyword argument.

To solve the error, use the yaml.full_load() method instead or explicitly set the Loader keyword arg.

 config = yaml.full_load(ymlfile)

or

config = yaml.load(ymlfile, Loader=yaml.FullLoader)
like image 1
Codemaker Avatar answered Oct 23 '22 13:10

Codemaker