462

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

$ 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)

harperville
  • 6,921
  • 8
  • 28
  • 36
  • add information on which platform do you use. libyaml. http://pyyaml.org/wiki/LibYAML is not pure python package, it may cause different installation. – Larry Cai Jul 31 '13 at 15:09
  • 1
    For anyone else on the confusion of naming, `pip install pyyaml` imports as `import yaml` inside your actual script. – kevin_theinfinityfund Feb 09 '22 at 18:43

12 Answers12

733

You could try the search feature in 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 docs recommend using PyYaml, since syck is out of date). Now you know a specific package name, you can install it:

$ pip install pyyaml

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

$ sudo apt-get install python-yaml
$ sudo yum install python-yaml
wim
  • 338,267
  • 99
  • 616
  • 750
Bonlenfum
  • 19,101
  • 2
  • 53
  • 56
  • 11
    The name of the PyYAML package at least on Fedora 21 and CentOS 7 is just `PyYAML` not `python-yaml`. – TNT Mar 27 '15 at 15:03
  • 3
    PyYAML implements the old YAML 1.1 spec from 2004 (Syck the even older 1.0 spec). ruamel.yaml is now the non-out-of-date package that implements the YAML1.2 specification – Anthon Mar 30 '16 at 19:19
  • So what if I am on a system with neither pip nor a package manager? – FirefoxMetzger Apr 27 '17 at 14:26
  • 1
    @FirefoxMetzger you can [get python](https://www.python.org/downloads/), [get pip](https://bootstrap.pypa.io/get-pip.py), [build LibYAML from source](http://pyyaml.org/wiki/LibYAML), and finally [install PyYAML](http://pyyaml.org/wiki/PyYAMLDocumentation). LibYAML is not strictly required as PyYAML will work with pure python (albeit slower) – tutuDajuju May 05 '17 at 07:51
  • thanks a lot ,your answer have solved my question entirely. – wolfog May 08 '18 at 08:08
  • I had to install it "system wide" using `apt`. With `pip` it was not working – Kaymaz May 31 '18 at 20:41
  • You teach how to catch a fish, instead of giving it away :) Thanks! – hopflink Sep 18 '18 at 07:23
  • The `pip search yaml` prints out 102 lines, none of which actually included the `pyyaml` package name, even though `pip install pyyaml` *does* work. Going through all those lines would have taken 50 times more than searching "pip install yaml" on Google. Not your fault, but unfortunately, today you are in 99% of the cases better off asking an internet search engine instead of using the intended lookup tools (also applies to `man`, for example). Thanks for showing how to do it anyway. – phil294 Jun 21 '20 at 12:57
  • 4
    @phil294 you are right - it is a shame. When I wrote this answer in 2013, there were about 10 results for `yaml` in PyPI; today there are >4500; >750 matches for "pyyaml". In the comments to [how does pip search work](https://stackoverflow.com/a/51269282), we find that pip only returns the first 100 results, due to the PyPI api. But that is an explanation for the problem, not a method to solve the issue of selecting an appropriate package sadly. Would be v happy for suggestions of what to incorporate in the answer – Bonlenfum Jun 22 '20 at 10:25
  • 1
    Wow - ten years and a day since this question was asked and nothing's changed. – keithpjolley Jan 12 '23 at 01:24
  • 2
    There was a change, @keithpjolley: `pip` doesn't allow search anymore. :D Another point is that many pytonistas seeem to prefer `ruamel.yaml`, instead of `pyyaml`. – Hilton Fernandes Jan 13 '23 at 20:07
  • just as a small note: `pip search` seems no longer to be supported - instead, it is recommended to search online with a browser after navigating to `https://pypi.org/search` – Andreas Rozek Apr 23 '23 at 06:24
127

pip install pyyaml

If you don't have pip, run easy_install pip to 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

Community
  • 1
  • 1
harperville
  • 6,921
  • 8
  • 28
  • 36
74

This answers if for MacOS

Update: Nowadays installing is done with pip, and for many users a wheel may be available (depending on your Mac and required version of PyYaml). In some cases libyaml is still required to build the C extension (on mac); this can be done with:

brew install libyaml
python -m pip install pyyaml

Outdated method:

For MacOSX (mavericks), the following works:

brew install libyaml
sudo python -m easy_install pyyaml
tutuDajuju
  • 10,307
  • 6
  • 65
  • 88
25
pip install PyYAML

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

wim
  • 338,267
  • 99
  • 616
  • 750
bbaassssiiee
  • 6,013
  • 2
  • 42
  • 55
  • On Ubuntu 14.04 LTS, I got the error `libyaml is not found or a compiler error: forcing --without-libyaml` when using `sudo pip install PyYAML`. What should I do? Thanks. – hengxin Jul 23 '14 at 11:26
  • 1
    However, I said `Successfully installed PyYAML`. See [pip-PyYAML](http://i1.tietuku.com/896d50da2caf8e06.png) for complete information. Thanks again. – hengxin Jul 23 '14 at 11:37
16

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.yaml which follows the latest (YAML 1.2, from 2009) specification.

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

sudo apt-get install python-ruamel.yaml
Anthon
  • 69,918
  • 32
  • 186
  • 246
10

following command will download pyyaml, which also includes yaml

pip install pyYaml
Skandix
  • 1,916
  • 6
  • 27
  • 36
Ankit Shukla
  • 313
  • 3
  • 11
8

Debian-based systems:

$ sudo aptitude install python-yaml

or newer for python3

$ sudo aptitude install python3-yaml

hbadger
  • 91
  • 1
  • 4
5

"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

Install the libYAML and it's headers:

sudo apt-get install libyaml-dev

Download the pyyaml sources:

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

Install from sources, (don't forget to activate your 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 
harperville
  • 6,921
  • 8
  • 28
  • 36
Sergey Belash
  • 1,433
  • 3
  • 16
  • 21
  • 2
    This was the only method that led me to correctly install the CLoaders on MacOS. I previously tried `brew install libyaml & pip install pyyaml` but, `python -c 'from yaml import CSafeLoader'` kept failing – Luis Meraz Oct 05 '19 at 22:14
3

Consider using strictyaml instead

If you have the luxury of creating the yaml file yourself, or if you don't require any of these features of regular yaml, I recommend using strictyaml instead of the standard pyyaml package.

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

You can read more about the problems with regular yaml here

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

Connor
  • 4,216
  • 2
  • 29
  • 40
2

️ in a virtual environment or using Python 2

pip install pyyaml

️ for python 3 (could also be pip3.10 depending on your version)

pip3 install pyyaml

️ if you get permissions error

sudo pip3 install pyyaml

️ if you don't have pip in your PATH environment variable

python -m pip install pyyaml

️ for python 3 (could also be pip3.10 depending on your version)

python3 -m pip install pyyaml

️ for Anaconda

conda install -c conda-forge pyyaml

hailuodev
  • 764
  • 5
  • 9
1

For me, installing development version of libyaml did it.

yum install libyaml-devel         #centos
apt-get install libyaml-dev       # ubuntu
Mayank Jaiswal
  • 12,338
  • 7
  • 39
  • 41
0

Type in pip3 install yaml or like Connor pip3 install strictyaml

  • Welcome to StackOverflow! You shouldn't answer old questions, especially if they already have 10 answers and are pretty old, unless you have something important to add, which does not seem the case here. Provide answers to newer questions, that way you will also be able to comment on posts (once you have sufficient reputation) – finnmglas Sep 15 '20 at 04:41
  • Sorry, I'm new to asking on Stackoverflow – Gurshan Sekhon Sep 15 '20 at 05:35
  • Don't worry, in February I was exactly where you are now... it takes some time to get used to the system but you can profit from it a lot : ) – finnmglas Sep 15 '20 at 06:44