Merge branch 'delete/virtualenv' into 'master'

remove(tools): Dropping of virtualenv support

Closes IDF-10945

See merge request espressif/esp-idf!40650
This commit is contained in:
Jakub Kocka
2025-07-25 17:53:02 +08:00

View File

@@ -2722,56 +2722,6 @@ def get_constraints(idf_version: str, online: bool = True) -> str:
raise SystemExit(1)
def install_legacy_python_virtualenv(path: str) -> None:
"""
Checks if pip is installed (and installs it if not),
checks whether virtualenv is already installed (and in which version),
and finally creates virtual environment with python -m virtualenv <virtualenv_options>.
"""
# Before creating the virtual environment, check if pip is installed.
try:
subprocess.check_call([sys.executable, '-m', 'pip', '--version'])
except subprocess.CalledProcessError:
fatal(
f"Python interpreter at {sys.executable} doesn't have pip installed. "
'Please check the Getting Started Guides for the steps to install prerequisites for your OS.'
)
raise SystemExit(1)
virtualenv_installed_via_pip = False
try:
import virtualenv # noqa: F401
except ImportError:
info('Installing virtualenv')
subprocess.check_call(
[sys.executable, '-m', 'pip', 'install', '--user', 'virtualenv'], stdout=sys.stdout, stderr=sys.stderr
)
virtualenv_installed_via_pip = True
# since we just installed virtualenv via pip, we know that version is recent enough
# so the version check below is not necessary.
with_seeder_option = True
if not virtualenv_installed_via_pip:
# virtualenv is already present in the system and may have been installed via OS package manager
# check the version to determine if we should add --seeder option
try:
major_ver = int(virtualenv.__version__.split('.')[0])
if major_ver < 20:
warn(f'Virtualenv version {virtualenv.__version__} is old, please consider upgrading it')
with_seeder_option = False
except (ValueError, NameError, AttributeError, IndexError):
pass
info(f'Creating a new Python environment using virtualenv in {path}')
virtualenv_options = ['--python', sys.executable]
if with_seeder_option:
virtualenv_options += ['--seeder', 'pip']
subprocess.check_call(
[sys.executable, '-m', 'virtualenv', *virtualenv_options, path], stdout=sys.stdout, stderr=sys.stderr
)
def action_install_python_env(args): # type: ignore
"""
(Re)installs python virtual environment.
@@ -2782,16 +2732,6 @@ def action_install_python_env(args): # type: ignore
reinstall = args.reinstall
idf_python_env_path, _, virtualenv_python, idf_version = get_python_env_path()
nix_store = os.environ.get('NIX_STORE')
is_nix = nix_store is not None and sys.base_prefix.startswith(nix_store) and sys.prefix.startswith(nix_store)
is_virtualenv = not is_nix and (
hasattr(sys, 'real_prefix') or (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix)
)
if is_virtualenv and (not os.path.exists(idf_python_env_path) or reinstall):
fatal('This script was called from a virtual environment, can not create a virtual environment again')
raise SystemExit(1)
if os.path.exists(virtualenv_python):
try:
subprocess.check_call([virtualenv_python, '--version'], stdout=sys.stdout, stderr=sys.stderr)
@@ -2821,17 +2761,8 @@ def action_install_python_env(args): # type: ignore
if os.path.exists(virtualenv_python):
check_python_venv_compatibility(idf_python_env_path, idf_version)
else:
if (
subprocess.run(
[sys.executable, '-m', 'venv', '-h'], check=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
).returncode
== 0
):
# venv available
virtualenv_options = []
info(f'Creating a new Python environment in {idf_python_env_path}')
try:
environ_idf_python_env_path = os.environ['IDF_PYTHON_ENV_PATH']
correct_env_path = environ_idf_python_env_path.endswith(
@@ -2847,7 +2778,6 @@ def action_install_python_env(args): # type: ignore
'recommended to re-run this script from a clean shell where an ESP-IDF environment is '
'not active.'
)
# Verify if IDF_PYTHON_ENV_PATH is a valid ESP-IDF Python virtual environment directory
# to decide if content should be removed
if os.path.exists(os.path.join(environ_idf_python_env_path, VENV_VER_FILE)) or re.search(
@@ -2864,13 +2794,11 @@ def action_install_python_env(args): # type: ignore
except KeyError:
# if IDF_PYTHON_ENV_PATH not defined then the above checks can be skipped
pass
subprocess.check_call(
[sys.executable, '-m', 'venv', *virtualenv_options, idf_python_env_path],
stdout=sys.stdout,
stderr=sys.stderr,
)
try:
with open(os.path.join(idf_python_env_path, VENV_VER_FILE), 'w', encoding='utf-8') as f:
f.write(idf_version)
@@ -2881,10 +2809,6 @@ def action_install_python_env(args): # type: ignore
'(Diagnostic information. It can be ignored.)'
)
else:
# The embeddable Python for Windows doesn't have the built-in venv module
install_legacy_python_virtualenv(idf_python_env_path)
env_copy = os.environ.copy()
# Enforce disabling possible pip 'user' option to prevent installation error with virtual environment
env_copy['PIP_USER'] = 'no'