Skip to content

Commit 6a48147

Browse files
davidsanfalczoido
andauthored
Pyenv (#4347)
* pip_env to py_env * wip * wip * wip * wip * wip * wip * wip * wip * Update pipenv.rst --------- Co-authored-by: Carlos Zoido <mrgalleta@gmail.com>
1 parent 82eab16 commit 6a48147

File tree

3 files changed

+97
-80
lines changed

3 files changed

+97
-80
lines changed

‎reference/tools/system.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ conan.tools.system
55
:maxdepth: 2
66

77
system/package_manager
8-
system/pipenv
8+
system/pyenv

‎reference/tools/system/pipenv.rst‎

Lines changed: 6 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,13 @@
1+
:orphan:
2+
13
.. _conan_tools_system_pipenv:
24

35

46
PipEnv
57
======
68

7-
.. include:: ../../../common/experimental_warning.inc
8-
9-
.. important::
10-
11-
This is **only** for:
12-
- **Executable Python packages and its Python dependencies.** (for example, meson build system)
13-
- **Python package used during the source build process.** (for example, html5lib as a build dependency)
14-
This approach doesn't work for Python library packages that you would typically use via ``import`` inside your recipe.
15-
16-
The ``PipEnv`` helper installs executable Python packages with **pip** inside a dedicated virtual environment (**venv**),
17-
keeping them isolated so they don't interfere with system packages or the Conan package itself.
18-
It is designed to use a Python CLI tool inside a recipe during the build step.
19-
20-
By default, it attempts to create the virtualenv using the Python you have set on your system Path.
21-
To use a different one, you can set a Python path in the ``tools.system.pipenv:python_interpreter`` :ref:`configuration<reference_config_files_global_conf>`.
22-
23-
24-
.. currentmodule:: conan.tools.system
25-
26-
.. autoclass:: PipEnv
27-
:members:
28-
:inherited-members:
29-
30-
Using a Python package in a recipe
31-
----------------------------------
32-
33-
To install a python package or use a tool installed with Python, we have to install it using the ``PipEnv.install()`` method.
34-
We also have to call the ``PipEnv.generate()`` method to create a **Conan Environment** that adds the **Python virtualenv path** to the system path.
35-
36-
These two steps appear in the following recipe in the ``generate()`` method.
37-
Calling it in this method ensures that the **Python package** and the **Conan Environment** will be available in the following steps.
38-
In this case, in the build step, which is where we will use the installed tool.
39-
40-
.. code-block:: python
41-
:caption: conanfile.py
42-
43-
from conan import ConanFile
44-
from conan.tools.system import PipEnv
45-
from conan.tools.layout import basic_layout
46-
47-
48-
class PipPackage(ConanFile):
49-
name = "pip_install"
50-
version = "0.1"
51-
52-
def layout(self):
53-
basic_layout(self)
54-
55-
def generate(self):
56-
PipEnv(self).install(["meson==1.9.1"])
57-
PipEnv(self).generate()
58-
59-
def build(self):
60-
self.run("meson --version")
61-
62-
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.
63-
64-
.. code-block:: bash
65-
66-
$ conan build .
67-
68-
...
69-
70-
======== Finalizing install (deploy, generators) ========
71-
conanfile.py (pip_install/0.1): Calling generate()
72-
conanfile.py (pip_install/0.1): Generators folder: /Users/user/pip_install/build/conan
73-
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
74-
Collecting meson==1.9.1
75-
Using cached meson-1.9.1-py3-none-any.whl.metadata (1.8 kB)
76-
Using cached meson-1.9.1-py3-none-any.whl (1.0 MB)
77-
Installing collected packages: meson
78-
Successfully installed meson-1.9.1
79-
80-
conanfile.py (pip_install/0.1): Generating aggregated env files
81-
conanfile.py (pip_install/0.1): Generated aggregated env files: ['conanbuild.sh', 'conanrun.sh']
9+
.. warning::
8210

83-
======== Calling build() ========
84-
conanfile.py (pip_install/0.1): Calling build()
85-
conanfile.py (pip_install/0.1): RUN: meson --version
86-
1.9.1
11+
Starting with Conan version 2.25.0, `PipEnv`` is deprecated and has been renamed to `PyEnv`.
12+
Please update your recipes to use `PyEnv`` to ensure future compatibility. You can find
13+
the :ref:`complete documentantion about PyEnv here <conan_tools_system_pyenv>`.

‎reference/tools/system/pyenv.rst‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
.. _conan_tools_system_pyenv:
2+
3+
4+
PyEnv
5+
=====
6+
7+
.. include:: ../../../common/experimental_warning.inc
8+
9+
.. important::
10+
11+
This is **only** for:
12+
- **Executable Python packages and its Python dependencies.** (for example, meson build system)
13+
- **Python package used during the source build process.** (for example, html5lib as a build dependency)
14+
This approach doesn't work for Python library packages that you would typically use via ``import`` inside your recipe.
15+
16+
The ``PyEnv`` helper installs executable Python packages with **pip** inside a dedicated virtual environment (**venv**),
17+
keeping them isolated so they don't interfere with system packages or the Conan package itself.
18+
It is designed to use a Python CLI tool inside a recipe during the build step.
19+
20+
By default, it attempts to create the virtualenv using the Python you have set on your system Path.
21+
To use a different one, you can set a Python path in the ``tools.system.pyenv:python_interpreter`` :ref:`configuration<reference_config_files_global_conf>`.
22+
23+
24+
.. currentmodule:: conan.tools.system
25+
26+
.. autoclass:: PyEnv
27+
:members:
28+
:inherited-members:
29+
30+
Using a Python package in a recipe
31+
----------------------------------
32+
33+
To install a python package or use a tool installed with Python, we have to install it using the ``PyEnv.install()`` method.
34+
35+
When the `py_version`` parameter is defined, Conan will automatically use `UV <https://docs.astral.sh/uv/>` to create and manage a temporary virtual environment
36+
with that specific Python version.
37+
38+
We also have to call the ``PyEnv.generate()`` method to create a **Conan Environment** that adds the **Python virtualenv path** to the system path.
39+
40+
These two steps appear in the following recipe in the ``generate()`` method.
41+
Calling it in this method ensures that the **Python package** and the **Conan Environment** will be available in the following steps.
42+
In this case, in the build step, which is where we will use the installed tool.
43+
44+
.. code-block:: python
45+
:caption: conanfile.py
46+
47+
from conan import ConanFile
48+
from conan.tools.system import PyEnv
49+
from conan.tools.layout import basic_layout
50+
51+
52+
class PipPackage(ConanFile):
53+
name = "pip_install"
54+
version = "0.1"
55+
56+
def layout(self):
57+
basic_layout(self)
58+
59+
def generate(self):
60+
PyEnv(self).install(["meson==1.9.1"])
61+
PyEnv(self).generate()
62+
63+
def build(self):
64+
self.run("meson --version")
65+
66+
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.
67+
68+
.. code-block:: bash
69+
70+
$ conan build .
71+
72+
...
73+
74+
======== Finalizing install (deploy, generators) ========
75+
conanfile.py (pip_install/0.1): Calling generate()
76+
conanfile.py (pip_install/0.1): Generators folder: /Users/user/pip_install/build/conan
77+
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
78+
Collecting meson==1.9.1
79+
Using cached meson-1.9.1-py3-none-any.whl.metadata (1.8 kB)
80+
Using cached meson-1.9.1-py3-none-any.whl (1.0 MB)
81+
Installing collected packages: meson
82+
Successfully installed meson-1.9.1
83+
84+
conanfile.py (pip_install/0.1): Generating aggregated env files
85+
conanfile.py (pip_install/0.1): Generated aggregated env files: ['conanbuild.sh', 'conanrun.sh']
86+
87+
======== Calling build() ========
88+
conanfile.py (pip_install/0.1): Calling build()
89+
conanfile.py (pip_install/0.1): RUN: meson --version
90+
1.9.1

0 commit comments

Comments
 (0)