如何为 Python 安装 yaml 包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14261614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:53:41  来源:igfitidea点击:

How do I install the yaml package for Python?

pythonpython-2.7yamlpippyyaml

提问by harperville

I have a Python program that uses YAML. I attempted to install it on a new server using pip install yamland it returns the following:

我有一个使用 YAML 的 Python 程序。我尝试使用它在新服务器上安装它pip install yaml,它返回以下内容:

$ sudo pip install yaml
Downloading/unpacking yaml
  Could not find any downloads that satisfy the requirement yaml
No distributions at all found for yaml
Storing complete log in /home/pa/.pip/pip.log

How do I install the yaml package for Python? I'm running Python 2.7. (OS: Debian Wheezy)

如何为 Python 安装 yaml 包?我正在运行 Python 2.7。(操作系统:Debian Wheezy)

采纳答案by Bonlenfum

You could try the search feature in pip,

你可以试试 pip 中的搜索功能,

$ pip search yaml

which looks for packages in PyPI with yaml in the short description. That reveals various packages, including PyYaml, yamltools, and PySyck, among others (Note that PySyck docsrecommend using PyYaml, since syck is out of date). Now you know a specific package name, you can install it:

它在简短描述中使用 yaml 在 PyPI 中查找包。这揭示了各种软件包,包括 PyYaml、yamltools 和 PySyck 等(请注意,PySyck 文档建议使用 PyYaml,因为 syck 已过时)。现在你知道了一个特定的包名,你可以安装它:

$ pip install pyyaml

If you want to install python yaml system-wide in linux, you can also use a package manager, like aptitudeor yum:

如果要在 linux 系统范围内安装 python yaml,还可以使用包管理器,例如aptitudeyum

$ sudo apt-get install python-yaml
$ sudo yum install python-yaml

回答by harperville

pip install pyyaml

pip install pyyaml

If you don't have pip, run easy_install pipto install pip, which is the go-to package installer - Why use pip over easy_install?. If you prefer to stick with easy_install, then easy_install pyyaml

如果您没有 pip,请运行easy_install pip以安装 pip,这是首选软件包安装程序 -为什么要使用 pip 而不是 easy_install?. 如果您更喜欢使用easy_install,那么easy_install pyyaml

回答by tutuDajuju

Update:Nowadays installing is done with pip, but libyaml is still required to build the C extension (on mac):

更新:现在安装是用 pip 完成的,但仍然需要 libyaml 来构建 C 扩展(在 mac 上):

brew install libyaml
python -m pip install pyyaml


Outdated method:

过时的方法

For MacOSX (mavericks), the following seems to work:

对于 MacOSX(小牛),以下似乎有效:

brew install libyaml
sudo python -m easy_install pyyaml

回答by bbaassssiiee

pip install PyYAML

If libyaml is not found or compiled PyYAML can do without it on Mavericks.

如果未找到或未编译 libyaml,PyYAML 可以在 Mavericks 上不用它。

回答by Anthon

There are three YAML capable packages. Syck (pip install syck) which implements the YAML 1.0 specification from 2002; PyYAML (pip install pyyaml) which follows the YAML 1.1 specification from 2004; and ruamel.yamlwhich follows the latest (YAML 1.2, from 2009) specification.

有三个支持 YAML 的包。Syck ( pip install syck) 从 2002 年开始实施 YAML 1.0 规范;PyYAML ( pip install pyyaml) 遵循 2004 年的 YAML 1.1 规范;和ruamel.yaml遵循最新的(YAML 1.2,从 2009 年开始)规范。

You can install the YAML 1.2 compatible package with pip install ruamel.yamlor if you are running a modern version of Debian/Ubuntu (or derivative) with:

pip install ruamel.yaml如果您正在运行现代版本的 Debian/Ubuntu(或衍生版本),您可以使用以下命令安装 YAML 1.2 兼容包:

sudo apt-get install python-ruamel.yaml

回答by hbadger

Debian-based systems:

基于 Debian 的系统:

$ sudo aptitude install python-yaml

$ sudo aptitude install python-yaml

or newer for python3

或更新的python3

$ sudo aptitude install python3-yaml

$ sudo aptitude install python3-yaml

回答by Mayank Jaiswal

For me, installing development version of libyaml did it.

对我来说,安装开发版的 libyaml 做到了。

yum install libyaml-devel         #centos
apt-get install libyaml-dev       # ubuntu

回答by Sergey Belash

"There should be one -- and preferably only one -- obvious way to do it." So let me add another one. This one is more like "install from sources" for Debian/Ubuntu, from https://github.com/yaml/pyyaml

“应该有一种——最好只有一种——明显的方法来做到这一点。” 所以让我再添加一个。这个更像是 Debian/Ubuntu 的“从源代码安装”,来自https://github.com/yaml/pyyaml

Install the libYAML and it's headers:

安装 libYAML 及其标头:

sudo apt-get install libyaml-dev

Downloadthe pyyaml sources:

下载pyyaml源:

wget http://pyyaml.org/download/pyyaml/PyYAML-3.13.tar.gz

Install from sources, (don't forget to activate your venv):

从源代码安装,(不要忘记激活你的 venv):

. your/env/bin/activate
tar xzf PyYAML-3.13.tar.gz
cd PyYAML-3.13.tar.gz
(env)$ python setup.py install
(env)$ python setup.py test 

回答by Ankit Shukla

following command will download pyyaml, which also includes yaml

以下命令将下载pyyaml,其中还包括yaml

pip install pyYaml

回答by Connor

Use strictyamlinstead

使用strictyaml代替

If you have the luxury of creating the yaml file yourself, or if you don't require any of these featuresof regular yaml, I recommend using strictyamlinstead of the standard pyyamlpackage.

如果您有幸自己创建 yaml 文件,或者如果您不需要常规 yaml 的任何这些功能,我建议使用strictyaml而不是标准pyyaml包。

In short, default yaml has some serious flaws in terms of security, interface, and predictability. strictyamlis a subset of the yaml spec that does not have those issues (and is better documented).

简而言之,default yaml 在安全性、接口和可预测性方面存在一些严重的缺陷。strictyaml是 yaml 规范的一个子集,没有这些问题(并且有更好的文档记录)。

You can read more about the problems with regular yaml here

您可以在此处阅读有关常规 yaml 问题的更多信息

OPINION:strictyamlshould be the default implementation of yaml and the old yaml spec should be obsoleted.

意见:strictyaml应该是 yaml 的默认实现,旧的 yaml 规范应该被废弃。