Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conan/internal/model/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
"tools.system.package_manager:mode": "Mode for package_manager tools: 'check', 'report', 'report-installed' or 'install'",
"tools.system.package_manager:sudo": "Use 'sudo' when invoking the package manager tools in Linux (False by default)",
"tools.system.package_manager:sudo_askpass": "Use the '-A' argument if using sudo in Linux to invoke the system package manager (False by default)",
"tools.system.pipenv:python_interpreter": "Path to the Python interpreter to be used to create the virtualenv",
"tools.apple:sdk_path": "Path to the SDK to be used",
"tools.apple:enable_bitcode": "(boolean) Enable/Disable Bitcode Apple Clang flags",
"tools.apple:enable_arc": "(boolean) Enable/Disable ARC Apple Clang flags",
Expand Down
18 changes: 16 additions & 2 deletions conan/tools/system/pip_manager.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import venv
import platform
import os
import shutil
import sys

from conan.tools.build import cmd_args_to_string
from conan.tools.env.environment import Environment
from conan.errors import ConanException


class PipEnv:
Expand All @@ -23,6 +25,18 @@ def generate(self):
env.prepend_path("PATH", self.bin_dir)
env.vars(self._conanfile).save_script(self.env_name)

def _create_venv(self):
python_interpreter = self._conanfile.conf.get(
"tools.system.pipenv:python_interpreter",
default=shutil.which('python') if platform.system() == "Windows" else shutil.which('python3'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the shutil.which() default to find the current interpreter if we are running in a venv already? Or it finds it directly in the system?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the one inside the virtualenv. But it's a python that's only used during recipe execution. Every time you run it, it's a new environment. In any case, I'm going to add that it gets the real path because in virtualenv python is a symlink.

if not python_interpreter:
raise ConanException("PipEnv could not find a Python executable path.")

try:
self._conanfile.run(cmd_args_to_string([python_interpreter, '-m', 'venv', self._env_dir]))
except ConanException:
raise ConanException("PipEnv could not create a Python virtual environment.")

def install(self, packages, pip_args=None):
"""
Will try to install the list of pip packages passed as a parameter.
Expand All @@ -34,7 +48,7 @@ def install(self, packages, pip_args=None):
:return: the return code of the executed pip command.
"""

venv.EnvBuilder(clear=True, with_pip=True).create(self._env_dir)
self._create_venv()
args = [self._python_exe, "-m", "pip", "install", "--disable-pip-version-check"]
if pip_args:
args += list(pip_args)
Expand Down
Loading