TypeError:load() 缺少 1 个必需的位置参数:Google Colab 中的“Loader”

问题描述 投票:0回答:5

我正在尝试在 Google Colab 中进行常规导入。
此导入到目前为止一直有效。
如果我尝试:

import plotly.express as px

import pingouin as pg

我收到错误:

---------------------------------------------------------------------------
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'

我认为这可能是Google Colab或某些已更新的基本实用程序包的问题,但我找不到解决方法。

python plotly typeerror google-colaboratory pyyaml
5个回答
89
投票

现在,

load()
函数需要参数
loader=Loader

如果您的 YAML 文件仅包含简单的 YAML(str、int、lists),请尝试使用

yaml.safe_load()
而不是
yaml.load()
。 如果您需要FullLoader,您可以使用
yaml.full_load()

从 pyyaml>=5.4 开始,没有发现任何严重漏洞,pyyaml 状态

来源:https://stackoverflow.com/a/1774043/13755823

yaml.safe_load() 应该始终是首选,除非您明确需要 按顺序提供的任意对象序列化/反序列化 以避免引入任意代码执行的可能性。

更多关于

yaml.load(input)
这里


55
投票

发现问题了。
我正在安装

pandas_profiling
,并且此软件包已更新
pyyaml
至版本 6.0,这与 Google Colab 当前导入软件包的方式不兼容。
因此,只需恢复到
pyyaml
版本 5.4.1 就可以解决问题。

欲了解更多信息,请查看

pyyaml
此处的版本。
请在 GitHub

中查看此问题和正式答案

################################################## #################
要在代码中恢复到

pyyaml
版本 5.4.1,请在软件包安装末尾添加下一行:

!pip install pyyaml==5.4.1

重要的是把它放在安装的最后,有些安装会改变

pyyaml
版本。


18
投票

这对我有用

config = yaml.load(ymlfile, Loader=yaml.Loader)

7
投票

当我们使用 yaml.load() 方法而不指定 Loader 关键字参数时,会出现 Python

“TypeError: load() miss 1 requiredpositional argument: 'Loader'”

要解决该错误,请改用

yaml.full_load()
方法或显式设置
Loader
关键字参数。

 config = yaml.full_load(ymlfile)

config = yaml.load(ymlfile, Loader=yaml.FullLoader)

0
投票

pip 安装 pyyaml==3.12

它适用于 python 3.10,并且您必须将构造函数文件从 collections.Hashable 更新为 collections.abc.Hashable 。

© www.soinside.com 2019 - 2024. All rights reserved.