简体   繁体   English

无法从Ruamel.yaml导入YAML

[英]Cannot import YAML from Ruamel.yaml

I am trying to use ruamel.yaml and am using this code straight from the ruamel's documentation but I get the error: 我正在尝试使用ruamel.yaml,并且直接从ruamel的文档中使用此代码,但出现错误:

Traceback (most recent call last): File "ruameltest.py", line 12, in yaml = YAML() NameError: name 'YAML' is not defined 追溯(最近一次呼叫最近):文件“ ruameltest.py”,第12行,在yaml中= YAML()NameError:未定义名称“ YAML”

I am using Linux (RHEL) 我正在使用Linux(RHEL)

Python 3.4.9 的Python 3.4.9

rpm package: python34-ruamel-yaml-0.13.14-1.el7.x86_64 rpm包:python34-ruamel-yaml-0.13.14-1.el7.x86_64

How could I fix this issue? 我该如何解决这个问题?

import sys
from ruamel.yaml import YAML

inp = """\
# example
name:
  # details
  family: Smith   # very common
  given: Alice    # one of the siblings
"""

yaml = YAML()
code = yaml.load(inp)
code['name']['given'] = 'Bob'

yaml.dump(code, sys.stdout)

Your system installed ruamel.yaml version is severely outdated, it is now (May 2019) at 0.15.96. 您系统上安装的ruamel.yaml版本已经严重过时,现在(2019年5月)为0.15.96。 The API that introduced the YAML class, that your program can't find was introduced two years ago. 引入YAML类的API是您的程序无法找到的,它是两年前引入的。

Additionally your Python (3.4) has been end-of-life since 2019-03-18. 此外,自2019-03-18起,您的Python(3.4)已停产。

You can either follow the old, PyYAML, API: 您可以遵循旧的PyYAML API:

import sys
from ruamel.yaml import RoundTripLoader, RoundTripDumper, load, dump

inp = """\
# example
name:
  # details
  family: Smith   # very common
  given: Alice    # one of the siblings
"""

data = load(inp, Loader=RoundTripLoader)
data['name']['given'] = 'Bob'
dump(data, sys.stdout, Dumper=RoundTripDumper)

which gives: 这使:

# example
name:
  # details
  family: Smith   # very common
  given: Bob      # one of the siblings

Please note that you load data from YAML not code . 请注意,您是从YAML而不是代码加载数据的

It would of course be better to use an up-to-date version of ruamel.yaml and a maintained version of Python. 当然,最好使用ruamel.yaml的最新版本和Python的维护版本。 If you cannot install a newer Python version, or compile it yourself, you still can install a newer version of ruamel.yaml : 如果您无法安装更新的Python版本或自己编译,则仍然可以安装ruamel.yaml的更新版本:

$ python3 -m venv ~/venv/yamltest
$ source ~/venv/yamltest/bin/activate
$ pip install -U pip
$ pip install 'ruamel.yaml<=0.15.94'

And then you should be able to use your program as is. 然后,您应该能够按原样使用程序。

newer versions, released after 0.15.94, do not include .whl files for Python 3.4, those versions will probably still work, but you might need to have a compiler installed to make use of the C extensions, and if you do you should just download and install Python 3.7 较新的版本(在0.15.94之后发布)不包含.whl于Python 3.4的.whl文件,这些版本可能仍然可以使用,但是您可能需要安装编译器才能使用C扩展,如果这样做,您应该下载并安装Python 3.7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM