-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Define the Python version used by PipEnv using UV to create the venv #19388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
davidsanfal
wants to merge
7
commits into
conan-io:develop2
Choose a base branch
from
davidsanfal:feature/uv#19297
base: develop2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| from conan.tools.system.pip_manager import PipEnv | ||
| from conan.tools.system.pip_manager import PipEnv, UVEnv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,7 +7,7 @@ | |||||
| from conan.errors import ConanException | ||||||
|
|
||||||
|
|
||||||
| class PipEnv: | ||||||
| class PythonVirtualEnv: | ||||||
|
|
||||||
| def __init__(self, conanfile, folder=None, name=""): | ||||||
| """ | ||||||
|
|
@@ -23,7 +23,30 @@ def __init__(self, conanfile, folder=None, name=""): | |||||
| self.bin_dir = os.path.join(self._env_dir, bins) | ||||||
| pyexe = "python.exe" if platform.system() == "Windows" else "python" | ||||||
| self._python_exe = os.path.join(self.bin_dir, pyexe) | ||||||
| self._create_venv() | ||||||
|
|
||||||
| @property | ||||||
| def python(self): | ||||||
| return self._get_env_python(self._env_dir) | ||||||
|
|
||||||
| @property | ||||||
| def _default_python(self): | ||||||
| _config_python = self._conanfile.conf.get("tools.system.pipenv:python_interpreter") | ||||||
| python = "python" if platform.system() == "Windows" else "python3" | ||||||
| default_python = shutil.which(python) | ||||||
| _system_python = os.path.realpath(default_python) if default_python else None | ||||||
| _python = _config_python or _system_python | ||||||
| if _python: | ||||||
| return _python | ||||||
| else: | ||||||
| raise ConanException("Conan could not find a Python executable path. Please, install " | ||||||
| "Python system-wide or set the " | ||||||
| "'tools.system.pipenv:python_interpreter' " | ||||||
| "conf to the full path of a Python executable") | ||||||
|
|
||||||
| @staticmethod | ||||||
| def _get_env_python(env_dir): | ||||||
| _env_bin_dir = os.path.join(env_dir, "Scripts" if platform.system() == "Windows" else "bin") | ||||||
| return os.path.join(_env_bin_dir, "python.exe" if platform.system() == "Windows" else "python") | ||||||
|
|
||||||
| def generate(self): | ||||||
| """ | ||||||
|
|
@@ -33,27 +56,8 @@ def generate(self): | |||||
| env.prepend_path("PATH", self.bin_dir) | ||||||
| env.vars(self._conanfile).save_script(self.env_name) | ||||||
|
|
||||||
| @staticmethod | ||||||
| def _default_python(): | ||||||
| python = "python" if platform.system() == "Windows" else "python3" | ||||||
| default_python = shutil.which(python) | ||||||
| return os.path.realpath(default_python) if default_python else None | ||||||
|
|
||||||
| def _create_venv(self): | ||||||
| python_interpreter = self._conanfile.conf.get("tools.system.pipenv:python_interpreter") | ||||||
| python_interpreter = python_interpreter or self._default_python() | ||||||
| if not python_interpreter: | ||||||
| raise ConanException("PipEnv could not find a Python executable path. Please, install " | ||||||
| "Python system-wide or set the " | ||||||
| "'tools.system.pipenv:python_interpreter' " | ||||||
| "conf to the full path of a Python executable") | ||||||
|
|
||||||
| try: | ||||||
| self._conanfile.run(cmd_args_to_string([python_interpreter, '-m', 'venv', | ||||||
| self._env_dir])) | ||||||
| except ConanException as e: | ||||||
| raise ConanException(f"PipEnv could not create a Python virtual " | ||||||
| f"environment using '{python_interpreter}': {e}") | ||||||
| def run(self, args): | ||||||
| return self._conanfile.run(cmd_args_to_string([self.python] + list(args))) | ||||||
|
|
||||||
| def install(self, packages, pip_args=None): | ||||||
| """ | ||||||
|
|
@@ -65,9 +69,66 @@ def install(self, packages, pip_args=None): | |||||
| Defaults to ``None``. | ||||||
| :return: the return code of the executed pip command. | ||||||
| """ | ||||||
| args = [self._python_exe, "-m", "pip", "install", "--disable-pip-version-check"] | ||||||
| args = ["-m", "pip", "install", "--disable-pip-version-check"] | ||||||
| if pip_args: | ||||||
| args += list(pip_args) | ||||||
| args += list(packages) | ||||||
| command = cmd_args_to_string(args) | ||||||
| return self._conanfile.run(command) | ||||||
| return self.run(args) | ||||||
|
|
||||||
|
|
||||||
| class PipEnv(PythonVirtualEnv): | ||||||
|
|
||||||
| def __init__(self, conanfile, folder=None, name=""): | ||||||
| """ | ||||||
| :param conanfile: The current conanfile "self" | ||||||
| :param folder: Optional folder, by default the "build_folder" | ||||||
| :param name: Optional name for the virtualenv, by default "conan_pipenv" | ||||||
| """ | ||||||
| super().__init__(conanfile, folder, name) | ||||||
| self._create_venv() | ||||||
|
|
||||||
| def _create_venv(self): | ||||||
| try: | ||||||
| self._conanfile.run(cmd_args_to_string([self._default_python, '-m', 'venv', | ||||||
| self._env_dir])) | ||||||
| except ConanException as e: | ||||||
| raise ConanException(f"PipEnv could not create a Python virtual " | ||||||
| f"environment using '{self._default_python}': {e}") | ||||||
|
|
||||||
|
|
||||||
| class UVEnv(PythonVirtualEnv): | ||||||
|
|
||||||
| def __init__(self, conanfile, py_version, folder=None, name=""): | ||||||
| """ | ||||||
| :param conanfile: The current conanfile "self" | ||||||
| :param folder: Optional folder, by default the "build_folder" | ||||||
| :param name: Optional name for the virtualenv, by default "conan_uvenv" | ||||||
| :param py_version: Optional python version for the virtualenv using UV | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here says it's optional but it's not |
||||||
| """ | ||||||
| super().__init__(conanfile, folder, name) | ||||||
| self._base_env_dir = os.path.abspath(os.path.join(folder or conanfile.build_folder)) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| self._uv_env_dir = os.path.join(self._base_env_dir, f"uv_{self.env_name}") | ||||||
| self._create_uv_venv(py_version) | ||||||
|
|
||||||
| def _create_uv_venv(self, py_version): | ||||||
| try: | ||||||
| self._conanfile.run(cmd_args_to_string( | ||||||
| [self._default_python, '-m', 'venv', self._uv_env_dir]) | ||||||
| ) | ||||||
| _python_exe = self._get_env_python(self._uv_env_dir) | ||||||
| self._conanfile.run(cmd_args_to_string( | ||||||
| [_python_exe, "-m", "pip", "install", "--disable-pip-version-check", "uv"]) | ||||||
| ) | ||||||
| self._conanfile.run(cmd_args_to_string( | ||||||
| [_python_exe, '-m', 'uv', 'venv', '--seed', '--python', py_version, self._env_dir]) | ||||||
| ) | ||||||
| self._conanfile.output.info(f"Virtual environment for Python " | ||||||
| f"{py_version} created successfully using UV.") | ||||||
| except Exception as e: | ||||||
| raise ConanException(f"UVEnv could not create a Python {py_version} virtual " | ||||||
| f"environment using UV and '{self._default_python}': {e}") | ||||||
|
|
||||||
| def uvx(self, args): | ||||||
| return self._conanfile.run( | ||||||
| cmd_args_to_string([self._get_env_python(self._uv_env_dir), '-m', "uv", "tool", "run"] + list(args)) | ||||||
| ) | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import textwrap | ||
| import platform | ||
| from conan.test.utils.tools import TestClient | ||
| from conan.internal.util.files import save_files | ||
| from conan.test.utils.test_files import temp_folder | ||
|
|
||
|
|
||
| def _create_py_hello_world(folder): | ||
| setup_py = textwrap.dedent(""" | ||
| from setuptools import setup, find_packages | ||
|
|
||
| setup( | ||
| name='hello', | ||
| version='0.1.0', | ||
| packages=find_packages(include=['hello', 'hello.*']), | ||
| entry_points={'console_scripts': ['hello-world = hello:hello']} | ||
| ) | ||
| """) | ||
| hello_py = textwrap.dedent(""" | ||
| def hello(): | ||
| print("Hello Test World!") | ||
| """) | ||
|
|
||
| save_files(folder, {"setup.py": setup_py, "hello/__init__.py": hello_py}) | ||
|
|
||
|
|
||
| def test_build_uv_manager(): | ||
|
|
||
| pip_package_folder = temp_folder(path_with_spaces=True) | ||
| _create_py_hello_world(pip_package_folder) | ||
| pip_package_folder = pip_package_folder.replace('\\', '/') | ||
|
|
||
| conanfile_pip = textwrap.dedent(f""" | ||
| from conan import ConanFile | ||
| from conan.tools.system import UVEnv | ||
| from conan.tools.layout import basic_layout | ||
| import platform | ||
| import os | ||
|
|
||
|
|
||
| class PipPackage(ConanFile): | ||
| name = "pip_hello_test" | ||
| version = "0.1" | ||
|
|
||
| def layout(self): | ||
| basic_layout(self) | ||
|
|
||
| def generate(self): | ||
| pip_env = UVEnv(self, py_version="3.11.6") | ||
| pip_env.install(["{pip_package_folder}"]) | ||
| pip_env.generate() | ||
| pip_env.run(["--version"]) | ||
|
|
||
| def build(self): | ||
| self.run("hello-world") | ||
| """) | ||
|
|
||
| client = TestClient(path_with_spaces=False) | ||
| # FIXME: the python shebang inside vitual env packages fails when using path_with_spaces | ||
| client.save({"pip/conanfile.py": conanfile_pip}) | ||
| client.run("build pip/conanfile.py") | ||
| assert "Using CPython 3.11.6" in client.out | ||
| assert "Creating virtual environment with seed packages" in client.out | ||
| assert "Virtual environment for Python 3.11.6 created successfully using UV." in client.out | ||
| if platform.system() == "Windows": | ||
| assert "python.exe --version\nPython 3.11.6" in client.out | ||
| else: | ||
| assert "python --version\nPython 3.11.6" in client.out | ||
| assert "RUN: hello-world" in client.out | ||
| assert "Hello Test World!" in client.out | ||
|
|
||
|
|
||
| def test_fail_build_uv_manager(): | ||
|
|
||
| pip_package_folder = temp_folder(path_with_spaces=True) | ||
| _create_py_hello_world(pip_package_folder) | ||
| pip_package_folder = pip_package_folder.replace('\\', '/') | ||
|
|
||
| conanfile_pip = textwrap.dedent(f""" | ||
| from conan import ConanFile | ||
| from conan.tools.system import UVEnv | ||
| from conan.tools.layout import basic_layout | ||
| import platform | ||
| import os | ||
|
|
||
|
|
||
| class PipPackage(ConanFile): | ||
| name = "pip_hello_test" | ||
| version = "0.1" | ||
|
|
||
| def layout(self): | ||
| basic_layout(self) | ||
|
|
||
| def generate(self): | ||
| pip_env = UVEnv(self, py_version="3.11.86") | ||
| pip_env.install(["{pip_package_folder}"]) | ||
| pip_env.generate() | ||
|
|
||
| def build(self): | ||
| self.run("hello-world") | ||
| """) | ||
|
|
||
| client = TestClient(path_with_spaces=False) | ||
| # FIXME: the python shebang inside vitual env packages fails when using path_with_spaces | ||
| client.save({"pip/conanfile.py": conanfile_pip}) | ||
| client.run("build pip/conanfile.py", assert_error=True) | ||
| print(client.out) | ||
| assert "UVEnv could not create a Python 3.11.86 virtual environment using UV" in client.out | ||
|
|
||
|
|
||
| def test_run_uvx(): | ||
|
|
||
| pip_package_folder = temp_folder(path_with_spaces=True) | ||
| _create_py_hello_world(pip_package_folder) | ||
| pip_package_folder = pip_package_folder.replace('\\', '/') | ||
|
|
||
| conanfile_pip = textwrap.dedent(f""" | ||
| from conan import ConanFile | ||
| from conan.tools.system import UVEnv | ||
| from conan.tools.layout import basic_layout | ||
| import platform | ||
| import os | ||
|
|
||
|
|
||
| class PipPackage(ConanFile): | ||
| name = "pip_hello_test" | ||
| version = "0.1" | ||
|
|
||
| def layout(self): | ||
| basic_layout(self) | ||
|
|
||
| def generate(self): | ||
| pip_env = UVEnv(self, py_version="3.11.6") | ||
| pip_env.install(["{pip_package_folder}"]) | ||
| pip_env.generate() | ||
| pip_env.uvx(["ruff==0.14.9", "--version"]) | ||
|
|
||
| def build(self): | ||
| self.run("hello-world") | ||
| """) | ||
|
|
||
| client = TestClient(path_with_spaces=False) | ||
| # FIXME: the python shebang inside vitual env packages fails when using path_with_spaces | ||
| client.save({"pip/conanfile.py": conanfile_pip}) | ||
| client.run("build pip/conanfile.py") | ||
| assert "Using CPython 3.11.6" in client.out | ||
| assert "Creating virtual environment with seed packages" in client.out | ||
| assert "Virtual environment for Python 3.11.6 created successfully using UV." in client.out | ||
| assert "ruff 0.14.9" in client.out | ||
| assert "RUN: hello-world" in client.out | ||
| assert "Hello Test World!" in client.out |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you considered not exposing a new
UVEnvclass? Because in the end uv is just the method we use for creating the environment but the functionality is the same for both exposed public classes. Maybe it should be a single class that you instance like this?If we keep the two different public classes (which I think is fine) maybe you should consider composition instead of inheritance for the common functionality.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could make sense, maybe that would also be a rename of the class to
PyEnv?We could do an alias first, deprecate the
PipEnvwith a warning to not break existing usage.