load() missing 1 required positional argument: ‘loader’

If you are working with YAML files in Python, you might encounter the error “load() missing 1 required positional argument: ‘loader’” when you use the yaml.load() method. This error means that you have not specified the loader argument, which the yaml requires.load() method to parse the YAML file. In this article, we will explain what causes this error, how to fix it, and some frequently asked questions about it.

What is the “load() missing 1 required positional argument: ‘loader'” error?

The “load() missing 1 required positional argument: ‘loader’” error is a TypeError that occurs when you use the yaml.load() method without specifying the loader argument. The loader argument is a class that defines how to parse the YAML file and what data types to use. For example, the yaml.FullLoader class parses the YAML file as a Python dictionary and supports all YAML tags and types.

The yaml.load() method has the following syntax:

!pip install PyYAML
import yaml
yaml.load(stream, Loader=None)

The stream parameter is the YAML file or string that you want to load. The Loader parameter is the class that you want to use to parse the stream. If you omit the Loader parameter, you will get the error “load() missing 1 required positional argument: ‘loader'”.

For example, if you have a YAML file called config.yaml with the following content:

name: John
age: 25
hobbies:
  - reading
  - coding
  - gaming

And you try to load it with the following code:

import yaml

with open('config.yaml') as f:
  data = yaml.load(f)

You will get the following error:

TypeError: load() missing 1 required positional argument: 'Loader'
load() missing 1 required positional argument: 'loader'

What causes the “load() missing 1 required positional argument: ‘loader'” error?

The “load() missing 1 required positional argument: ‘loader'” error is caused by the change in the behavior of the yaml.load() method in PyYAML version 5.1 and later. PyYAML is a Python library that provides support for YAML files. Before version 5.1, the yaml.load() method used the yaml.Loader class as the default loader, which was unsafe and could execute arbitrary code. To prevent this security risk, the yaml.load() method now requires you to explicitly specify the loader argument.

The yaml.load() method supports several loader classes, each with different features and limitations. The most commonly used ones are:

  • yaml.FullLoader: This is the recommended loader class for most use cases. It parses the YAML file as a Python dictionary and supports all YAML tags and types. It is also safe and does not execute arbitrary code.
  •  yaml.SafeLoader: This is a more restrictive loader class that only supports the standard YAML tags and types. It is also safe and does not execute arbitrary code. It is useful for loading untrusted YAML files.
  •  yaml.UnsafeLoader: This is the legacy loader class that was used before version 5.1. It supports all YAML tags and types, but it is unsafe and can execute arbitrary code. It is not recommended for use unless you trust the source of the YAML file.

How do you resolve the “load() missing 1 required positional argument: ‘loader'” error?

To resolve the “load() missing 1 required positional argument: ‘loader’” error, you need to specify the loader argument when you use the yaml.load() method. The best way to do this is to use the yaml.full_load() method, which is equivalent to using the yaml.load() method with the yaml.FullLoader class. The yaml.full_load() method has the following syntax:

!pip install PyYAML
import yaml
stream = open('example.yaml', 'r')
data = yaml.full_load(stream)
stream.close()

The stream parameter is the same as the stream parameter of the yaml.load() method. The yaml.full_load() method returns a Python dictionary that represents the YAML file or string.

For example, to load the config.yaml file from the previous section, you can use the following code:

import yaml

with open('config.yaml') as f:
  data = yaml.full_load(f)

print(data)

This will output:

{'name': 'John', 'age': 25, 'hobbies': ['reading', 'coding', 'gaming']}

Alternatively, you can use the yaml.safe_load() method or the yaml.load() method with the yaml.SafeLoader class if you want to load untrusted YAML files. For example:

import yaml

with open('config.yaml') as f:
  data = yaml.safe_load(f)

# or

with open('config.yaml') as f:
  data = yaml.load(f, Loader=yaml.SafeLoader)

However, note that the yaml.safe_load() method and the yaml.SafeLoader class does not support all YAML tags and types, so they might raise an exception if the YAML file contains unsupported features.

You should avoid using the yaml.unsafe_load() method or the yaml.load() method with the yaml.UnsafeLoader class, as they can execute arbitrary code and pose a security risk. For example, if the YAML file contains the following malicious code:

!!python/object/apply:os.system ["echo hello"]

The yaml.unsafe_load() method or the yaml.load() method with the yaml.UnsafeLoader class will execute the os.system() function and print “hello” to the standard output. This could be exploited to run harmful commands on your system.

FAQs

What is YAML?

YAML is used commonly for configuration files and is a human-readable data serialization language. YAML stands for YAML Ain’t Markup Language.

What is PyYAML?

PyYAML is a Python library that provides support for YAML files. It allows you to load and dump YAML files or strings using various methods and classes.

How do I install PyYAML?

You can install PyYAML using pip, the Python package manager. To install PyYAML, run the following command in your terminal:
pip install pyyaml

How do I check the version of PyYAML?

You can check the version of PyYAML using the version attribute of the yaml module. To check the version of PyYAML, run the following code in your Python interpreter:
import yaml
print(yaml.version)

Conclusion

In conclusion, we have learned how to deal with the “load() missing 1 required positional argument: ‘loader’” error in Python. This error happens when we use the yaml.load() method to load a YAML file or string without specifying the loader argument. The loader argument is a class that tells the yaml.load() method, how to parse the YAML file, and what data types to use.

There are different loader classes available, such as yaml.FullLoader, yaml.SafeLoader, and yaml.UnsafeLoader, each with different features and limitations. The best way to avoid the error is to use the yaml.full_load() method, which uses the yaml.FullLoader class by default. This class parses the YAML file as a Python dictionary and supports all YAML tags and types.

It is also safe and does not execute arbitrary code. If we want to load untrusted YAML files, we can use the yaml.safe_load() method or the yaml.load() method with the yaml.SafeLoader class, which is more restrictive and only supports the standard YAML tags and types.

We should never use the yaml.unsafe_load() method or the yaml.load() method with the yaml.UnsafeLoader class, as they are unsafe and can execute arbitrary code. We have also answered some frequently asked questions about YAML, PyYAML, and the error. We hope this article has helped you understand and fix the error.

References

  1. PyYAML
  2. Loader

Follow us at PythonClear to learn more about solutions to general errors one may encounter while programming in Python.

Leave a Comment