Questions tagged [pyyaml]

PyYAML is a YAML 1.1 parser and emitter for Python. Use this tag for PyYAML specific python questions.

PyYAML is a parser/dumper for YAML 1.1 for the Python programming language.

PyYAML does not yet implemented the feature changes from the YAML 1.2 specification released in 2009.

See http://pyyaml.org/wiki/PyYAML for background and documentation.

Source code: https://github.com/yaml/pyyaml

828 questions
462
votes
12 answers

How do I install the yaml package for Python?

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…
harperville
  • 6,921
  • 8
  • 28
  • 36
181
votes
2 answers

How can I write data in YAML format in a file?

I need to write the below data to yaml file using Python: {A:a, B:{C:c, D:d, E:e}} i.e., dictionary in a dictionary. How can I achieve this?
user1643521
  • 2,191
  • 3
  • 14
  • 6
150
votes
8 answers

In Python, how can you load YAML mappings as OrderedDicts?

I'd like to get PyYAML's loader to load mappings (and ordered mappings) into the Python 2.7+ OrderedDict type, instead of the vanilla dict and the list of pairs it currently uses. What's the best way to do that?
Eric Naeseth
  • 2,293
  • 2
  • 19
  • 18
138
votes
10 answers

ImportError: No module named 'yaml'

I have one script in which I am trying to execute python3 env/common_config/add_imagepullsecret.py But, I am getting the following error: [root@kevin]# python3 env/common_config/add_imagepullsecret.py Traceback (most recent call last): File…
Neeraj
  • 1,481
  • 2
  • 7
  • 3
113
votes
9 answers

How to upgrade disutils package PyYAML?

I was trying to install chatterbot which has a dependency on PyYAML=3.12. In my Ubuntu machine installed PyYAML version is 3.11. So I used the following command to upgrade PyYAML: sudo -H pip3 install --upgrade PyYAML But it gives the following…
sphoenix
  • 3,327
  • 5
  • 22
  • 40
102
votes
9 answers

Can PyYAML dump dict items in non-alphabetical order?

I'm using yaml.dump to output a dict. It prints out each item in alphabetical order based on the key. >>> d = {"z":0,"y":0,"x":0} >>> yaml.dump( d, default_flow_style=False ) 'x: 0\ny: 0\nz: 0\n' Is there a way to control the order of the…
mwcz
  • 8,949
  • 10
  • 42
  • 63
94
votes
2 answers

Building an array of dictionary items in YAML?

Basically trying to something in yaml that could be done using this json: { models: [ { model: "a" type: "x" #bunch of properties... }, { model: "b" type: "y" #bunch of properties... } ] } So far this is what I…
sadaf2605
  • 7,332
  • 8
  • 60
  • 103
81
votes
1 answer

Reading YAML file with Python results in yaml.composer.ComposerError: expected a single document in the stream

I have a yaml file that looks like --- level_1: "test" level_2: 'NetApp, SOFS, ZFS Creation' request: 341570 --- level_1: "test" level_2: 'NetApp, SOFS, ZFS Creation' request: 341569 --- level_1: "test" level_2: 'NetApp, SOFS, ZFS Creation' request:…
Sriharsha
  • 2,373
  • 1
  • 16
  • 20
76
votes
4 answers

Save/dump a YAML file with comments in PyYAML

I have a yaml file that looks like this: # The following key opens a door key: value Is there a way I can load and dump this data while maintaining the comment?
Harley Holcombe
  • 175,848
  • 15
  • 70
  • 63
75
votes
1 answer

I don't understand what a YAML tag is

I get it on some level, but I have yet to see an example that didn't bring up more questions than answers. http://rhnh.net/2011/01/31/yaml-tutorial # Set.new([1,2]).to_yaml --- !ruby/object:Set hash: 1: true 2: true I get that we're declaring…
Fred
  • 3,786
  • 7
  • 41
  • 52
72
votes
5 answers

pyyaml: dumping without tags

I have >>> import yaml >>> yaml.dump(u'abc') "!!python/unicode 'abc'\n" But I want >>> import yaml >>> yaml.dump(u'abc', magic='something') 'abc\n' What magic param forces no tagging?
Paul Tarjan
  • 48,968
  • 59
  • 172
  • 213
59
votes
6 answers

How can I control what scalar form PyYAML uses for my data?

I've got an object with a short string attribute, and a long multi-line string attribute. I want to write the short string as a YAML quoted scalar, and the multi-line string as a literal scalar: my_obj.short = "Hello" my_obj.long =…
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
59
votes
4 answers

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…
user17147261
  • 1,131
  • 1
  • 4
  • 8
58
votes
3 answers

PyYAML dump format

I know there are a few questions about this on StackOverflow, but I couldn't find what I was looking for. I'm using pyyaml to read (.load()) a .yml file, modify or add a key, and then write it (.dump()) again. The problem is that I want to keep the…
nicosantangelo
  • 13,216
  • 3
  • 33
  • 47
49
votes
7 answers

Docker-compose no longer building image (AttributeError: cython_sources)

I am building a django-react site and suddenly my docker-compose no longer builds despite any changes to requirements or image versions. My requirements.txt looks as…
Marco Sousa
  • 659
  • 1
  • 6
  • 12
1
2 3
55 56