Path: blob/master/venv/Lib/site-packages/pip/_internal/utils/virtualenv.py
811 views
from __future__ import absolute_import12import logging3import os4import re5import site6import sys78from pip._internal.utils.typing import MYPY_CHECK_RUNNING910if MYPY_CHECK_RUNNING:11from typing import List, Optional1213logger = logging.getLogger(__name__)14_INCLUDE_SYSTEM_SITE_PACKAGES_REGEX = re.compile(15r"include-system-site-packages\s*=\s*(?P<value>true|false)"16)171819def _running_under_venv():20# type: () -> bool21"""Checks if sys.base_prefix and sys.prefix match.2223This handles PEP 405 compliant virtual environments.24"""25return sys.prefix != getattr(sys, "base_prefix", sys.prefix)262728def _running_under_regular_virtualenv():29# type: () -> bool30"""Checks if sys.real_prefix is set.3132This handles virtual environments created with pypa's virtualenv.33"""34# pypa/virtualenv case35return hasattr(sys, 'real_prefix')363738def running_under_virtualenv():39# type: () -> bool40"""Return True if we're running inside a virtualenv, False otherwise.41"""42return _running_under_venv() or _running_under_regular_virtualenv()434445def _get_pyvenv_cfg_lines():46# type: () -> Optional[List[str]]47"""Reads {sys.prefix}/pyvenv.cfg and returns its contents as list of lines4849Returns None, if it could not read/access the file.50"""51pyvenv_cfg_file = os.path.join(sys.prefix, 'pyvenv.cfg')52try:53with open(pyvenv_cfg_file) as f:54return f.read().splitlines() # avoids trailing newlines55except IOError:56return None575859def _no_global_under_venv():60# type: () -> bool61"""Check `{sys.prefix}/pyvenv.cfg` for system site-packages inclusion6263PEP 405 specifies that when system site-packages are not supposed to be64visible from a virtual environment, `pyvenv.cfg` must contain the following65line:6667include-system-site-packages = false6869Additionally, log a warning if accessing the file fails.70"""71cfg_lines = _get_pyvenv_cfg_lines()72if cfg_lines is None:73# We're not in a "sane" venv, so assume there is no system74# site-packages access (since that's PEP 405's default state).75logger.warning(76"Could not access 'pyvenv.cfg' despite a virtual environment "77"being active. Assuming global site-packages is not accessible "78"in this environment."79)80return True8182for line in cfg_lines:83match = _INCLUDE_SYSTEM_SITE_PACKAGES_REGEX.match(line)84if match is not None and match.group('value') == 'false':85return True86return False878889def _no_global_under_regular_virtualenv():90# type: () -> bool91"""Check if "no-global-site-packages.txt" exists beside site.py9293This mirrors logic in pypa/virtualenv for determining whether system94site-packages are visible in the virtual environment.95"""96site_mod_dir = os.path.dirname(os.path.abspath(site.__file__))97no_global_site_packages_file = os.path.join(98site_mod_dir, 'no-global-site-packages.txt',99)100return os.path.exists(no_global_site_packages_file)101102103def virtualenv_no_global():104# type: () -> bool105"""Returns a boolean, whether running in venv with no system site-packages.106"""107# PEP 405 compliance needs to be checked first since virtualenv >=20 would108# return True for both checks, but is only able to use the PEP 405 config.109if _running_under_venv():110return _no_global_under_venv()111112if _running_under_regular_virtualenv():113return _no_global_under_regular_virtualenv()114115return False116117118