PipEnv

Warning

This feature is experimental and subject to breaking changes. See the Conan stability section for more information.

Important

This is only for executable Python packages and its Python dependencies. This approach doesn’t work for Python library packages that you would typically use via import inside your recipe.

The PipEnv helper installs executable Python packages with pip inside a dedicated virtual environment (venv), keeping them isolated so they don’t interfere with system packages or the Conan package itself. It is designed to use a Python CLI tool inside a recipe during the build step.

class PipEnv(conanfile, folder=None)
generate()

Create a conan environment to use the python venv in the next steps of the conanfile.

install(packages, pip_args=None)

Will try to install the list of pip packages passed as a parameter.

Parameters:
  • packages – try to install the list of pip packages passed as a parameter.

  • pip_args – additional argument list to be passed to the ‘pip install’ command, for example: [’–no-cache-dir’, ‘–index-url’, ‘https://my.pypi.org/simple’]. Defaults to None.

Returns:

the return code of the executed pip command.

Using a Python package in a recipe

To use a tool installed with Python, we have to install it using the PipEnv.install() method. We also have to call the PipEnv.generate() method to create a Conan Environment that adds the Python virtualenv path to the system path.

These two steps appear in the following recipe in the generate() method. Calling it in this method ensures that the Python package and the Conan Environment will be available in the following steps. In this case, in the build step, which is where we will use the installed tool.

conanfile.py
from conan import ConanFile
from conan.tools.system import PipEnv
from conan.tools.layout import basic_layout


class PipPackage(ConanFile):
    name = "pip_install"
    version = "0.1"

    def layout(self):
        basic_layout(self)

    def generate(self):
        PipEnv(self).install(["meson==1.9.1"])
        PipEnv(self).generate()

    def build(self):
        self.run("meson --version")

If we run a conan build we can see how our Python package is installed when the generate step, and how it is called in the build step as if it were installed on the system.

$ conan build .

...

======== Finalizing install (deploy, generators) ========
conanfile.py (pip_install/0.1): Calling generate()
conanfile.py (pip_install/0.1): Generators folder: /Users/user/pip_install/build/conan
conanfile.py (pip_install/0.1): RUN: /Users/user/pip_install/build/pip_venv_pip_install/bin/python -m pip install --disable-pip-version-check meson==1.9.1
Collecting meson==1.9.1
  Using cached meson-1.9.1-py3-none-any.whl.metadata (1.8 kB)
Using cached meson-1.9.1-py3-none-any.whl (1.0 MB)
Installing collected packages: meson
Successfully installed meson-1.9.1

conanfile.py (pip_install/0.1): Generating aggregated env files
conanfile.py (pip_install/0.1): Generated aggregated env files: ['conanbuild.sh', 'conanrun.sh']

======== Calling build() ========
conanfile.py (pip_install/0.1): Calling build()
conanfile.py (pip_install/0.1): RUN: meson --version
1.9.1