Python requires

Introduction

The python_requires feature is a very convenient way to share files and code between different recipes. A python require is a special recipe that does not create packages and it is just intended to be reused by other recipes.

A very simple recipe that we want to reuse could be:

  1. from conan import ConanFile
  2. myvar = 123
  3. def myfunct():
  4. return 234
  5. class Pkg(ConanFile):
  6. name = "pyreq"
  7. version = "0.1"
  8. package_type = "python-require"

And then we will make it available to other packages with conan create .. Note that a python-require package does not create binaries, it is just the recipe part.

  1. $ conan create .
  2. # It will only export the recipe, but will NOT create binaries
  3. # python-requires do NOT have binaries

We can reuse the above recipe functionality declaring the dependency in the python_requires attribute and we can access its members using self.python_requires["<name>"].module:

  1. from conan import ConanFile
  2. class Pkg(ConanFile):
  3. name = "pkg"
  4. version = "0.1"
  5. python_requires = "pyreq/0.1"
  6. def build(self):
  7. v = self.python_requires["pyreq"].module.myvar # v will be 123
  8. f = self.python_requires["pyreq"].module.myfunct() # f will be 234
  9. self.output.info(f"{v}, {f}")
  1. $ conan create .
  2. ...
  3. pkg/0.1: 123, 234

Python requires can also use version ranges, and this can be recommended in many cases if those python-requires need to evolve over time:

  1. from conan import ConanFile
  2. class Pkg(ConanFile):
  3. python_requires = "pyreq/[>=1.0 <2]"

It is also possible to require more than 1 python-requires with python_requires = "pyreq/0.1", "other/1.2"

Extending base classes

A common use case would be to declare a base class with methods we want to reuse in several recipes via inheritance. We’d write this base class in a python-requires package:

  1. from conan import ConanFile
  2. class MyBase:
  3. def source(self):
  4. self.output.info("My cool source!")
  5. def build(self):
  6. self.output.info("My cool build!")
  7. def package(self):
  8. self.output.info("My cool package!")
  9. def package_info(self):
  10. self.output.info("My cool package_info!")
  11. class PyReq(ConanFile):
  12. name = "pyreq"
  13. version = "0.1"
  14. package_type = "python-require"

And make it available for reuse with:

  1. $ conan create .

Note that there are two classes in the recipe file:

  • MyBase is the one intended for inheritance and doesn’t extend ConanFile.

  • PyReq is the one that defines the current package being exported, it is the recipe for the reference pyreq/0.1.

Once the package with the base class we want to reuse is available we can use it in other recipes to inherit the functionality from that base class. We’d need to declare the python_requires as we did before and we’d need to tell Conan the base classes to use in the attribute python_requires_extend. Here our recipe will inherit from the class MyBase:

  1. from conan import ConanFile
  2. class Pkg(ConanFile):
  3. name = "pkg"
  4. version = "0.1"
  5. python_requires = "pyreq/0.1"
  6. python_requires_extend = "pyreq.MyBase"

The resulting inheritance is equivalent to declare our Pkg class as class Pkg(pyreq.MyBase, ConanFile). So creating the package we can see how the methods from the base class are reused:

  1. $ conan create .
  2. ...
  3. pkg/0.1: My cool source!
  4. pkg/0.1: My cool build!
  5. pkg/0.1: My cool package!
  6. pkg/0.1: My cool package_info!
  7. ...

In general, base class attributes are not inherited, and should be avoided as much as possible. There are method alternatives to some of them like export() or set_version(). For exceptional situations, see the init() method documentation for more information to extend inherited attributes.

It is possible to re-implement some of the base class methods, and also to call the base class method explicitly, with the Python super() syntax:

  1. from conan import ConanFile
  2. class Pkg(ConanFile):
  3. name = "pkg"
  4. version = "0.1"
  5. python_requires = "pyreq/0.1"
  6. python_requires_extend = "pyreq.MyBase"
  7. def source(self):
  8. super().source() # call the base class method
  9. self.output.info("MY OWN SOURCE") # Your own implementation

It is not mandatory to call the base class method, a full overwrite without calling super() is possible. Also the call order can be changed, and calling your own code, then super() is possible.

Reusing files

It is possible to access the files exported by a recipe that is used with python_requires. We could have this recipe, together with a myfile.txt file containing the “Hello” text.

  1. from conan import ConanFile
  2. class PyReq(ConanFile):
  3. name = "pyreq"
  4. version = "1.0"
  5. package_type = "python-require"
  6. exports = "*"
  1. $ echo "Hello" > myfile.txt
  2. $ conan create .

Now that the python-require has been created, we can access its path (the place where myfile.txt is) with the path attribute:

  1. import os
  2. from conan import ConanFile
  3. from conan.tools.files import load
  4. class Pkg(ConanFile):
  5. python_requires = "pyreq/0.1"
  6. def build(self):
  7. pyreq_path = self.python_requires["pyreq"].path
  8. myfile_path = os.path.join(pyreq_path, "myfile.txt")
  9. content = load(self, myfile_path) # content = "Hello"
  10. self.output.info(content)
  11. # we could also copy the file, instead of reading it

Note that only exports works for this case, but not exports_sources.

Testing python-requires

It is possible to test with test_package a python_require, by adding a test_package/conanfile.py:

conanfile.py

  1. from conan import ConanFile
  2. def mynumber():
  3. return 42
  4. class PyReq(ConanFile):
  5. name = "pyreq"
  6. version = "1.0"
  7. package_type = "python-require"

test_package/conanfile.py

  1. from conan import ConanFile
  2. class Tool(ConanFile):
  3. # Literal "tested_reference_str", Conan will dynamically replace it
  4. python_requires = "tested_reference_str"
  5. def test(self):
  6. pyreq = self.python_requires["pyreq"].module
  7. mynumber = pyreq.mynumber()
  8. self.output.info("{}!!!".format(mynumber))

The python_requires = "tested_reference_str" is mandatory from Conan 2.1. Automatic injection of python_requires without this declaration is deprecated and it will be removed in future versions.

Note that the test_package/conanfile.py does not need any type of declaration of the python_requires, this is done automatically and implicitly. We can now create and test it with:

  1. $ conan create .
  2. ...
  3. pyreq/0.1 (test package): 42!!!

Effect in package_id

The python_requires will affect the package_id of the consumer packages using those dependencies. By default, the policy is minor_mode, which means:

  • Changes to the patch version of the revision of a python-require will not affect the package ID. So depending on "pyreq/1.2.3" or "pyreq/1.2.4" will result in identical package ID (both will be mapped to "pyreq/1.2.Z" in the hash computation). Bump the patch version if you want to change your common code, but you don’t want the consumers to be affected or to fire a re-build of the dependants.

  • Changes to the minor version will produce a different package ID. So if you depend on "pyreq/1.2.3", and you bump the version to "pyreq/1.3.0", then, you will need to build new binaries that are using that new python-require. Bump the minor or major version if you want to make sure that packages requiring this python-require will be built using these changes in the code.

In most cases using a version-range python_requires = "pyreq/[>=1.0 <2.0]" is the right approach, because that means the major version bumps are not included because they would require changes in the consumers themselves. It is then possible to release a new major version of the pyreq/2.0, and have consumers gradually change their requirements to python_requires = "pyreq/[>=2.0 <3.0]", fix the recipes, and move forward without breaking the whole project.

As with the regular requires, this default can be customized with the core.package_id:default_python_mode configuration.

It is also possible to customize the effect of python_requires per package, using the package_id() method:

  1. from conan import ConanFile
  2. class Pkg(ConanFile):
  3. python_requires ="pyreq/[>=1.0]"
  4. def package_id(self):
  5. self.info.python_requires.patch_mode()

Resolution of python_requires

There are few important things that should be taken into account when using python_requires:

  • Python requires recipes are loaded by the interpreter just once, and they are common to all consumers. Do not use any global state in the python_requires recipes.

  • Python requires are private to the consumers. They are not transitive. Different consumers can require different versions of the same python-require. Being private, they cannot be overridden from downstream in any way.

  • python_requires cannot use regular requires or tool_requires themselves. Having a requirements() (and similar) methods to be inherited by recipes is possible and allowed, but the python_requires class itself cannot use them.

  • python_requires cannot be “aliased”.

  • python_requires can use native python import to other python files, as long as these are exported together with the recipe.

  • python_requires can be used as editable packages too.

  • python_requires are locked in lockfiles, to guarantee reproducibility, in the same way that other requires and tool_requires are locked.

Note

Best practices

  • Even if python-requires can python_requires transitively other python-requires recipes, this is discouraged. Multiple level inheritance and reuse can become quite complex and difficult to manage, it is recommended to keep the hierarchy flat.

  • Do not try to mix Python inheritance with python_requires_extend inheritance mechanisms, they are incompatible and can break.

  • Do not use multiple inheritance for python-requires