import logging import os import shutil import site import sys import sysconfig from shutil import which
# The `pkg_resources` module is provided by `setuptools`, which is itself a # dependency of `virtualenv`. Tolerate its absence so that this module may be # evaluated when that module is not available. Because users may not recognize # the `pkg_resources` module by name, raise a more descriptive error if it is # referenced during execution. try: import pkg_resources as _pkg_resources
get_pkg_resources = lambda: _pkg_resources except ImportError: def get_pkg_resources(): raise ValueError("The Python module `virtualenv` is not installed.")
@property def exists(self): # We need to check also for lib_path because different python versions # create different library paths. return os.path.isdir(self.path) and os.path.isdir(self.lib_path)
@property def broken_link(self): # We shouldn't ever be using virtualenv, but for historic reasons check this
virtualenv_python_link = os.path.join(self.path, ".Python") if os.path.lexists(virtualenv_python_link) andnot os.path.exists(virtualenv_python_link): returnTrue
# This isn't a broken link, but we can't run the below in this state ifnot os.path.exists(self.bin_path): returnTrue
with os.scandir(self.bin_path) as it: for entry in it: # There is no entry.exists(), it's not quite Path-like enough. if entry.is_symlink() andnot os.path.exists(entry.path): returnTrue
returnFalse
def create(self): if os.path.exists(self.path):
logger.warning(f"Removing existing venv at {self.path!r}")
shutil.rmtree(self.path, ignore_errors=True)
self._working_set = None
logger.info(f"Creating new venv at {self.path!r}")
call(*self.virtualenv, self.path)
def get_paths(self): """Wrapper around sysconfig.get_paths(), returning the appropriate paths for the env.""" if"venv"in sysconfig.get_scheme_names(): # This should always be used on Python 3.11 and above.
scheme = "venv" elif os.name == "nt": # This matches nt_venv, unless sysconfig has been modified.
scheme = "nt" elif os.name == "posix": # This matches posix_venv, unless sysconfig has been modified.
scheme = "posix_prefix" elif sys.version_info >= (3, 10): # Using the default scheme is somewhat fragile, as various Python # distributors (e.g., what Debian and Fedora package, and what Xcode # includes) change the default scheme away from the upstream # defaults, but it's about as good as we can do.
scheme = sysconfig.get_default_scheme() else: # This is explicitly documented as having previously existed in the 3.10 # docs, and has existed since CPython 2.7 and 3.1 (but not 3.0).
scheme = sysconfig._get_default_scheme()
@property def pip_path(self):
path = which("pip3", path=self.bin_path) if path isNone:
path = which("pip", path=self.bin_path) if path isNone: raise ValueError("pip3 or pip not found") return path
@property def lib_path(self): # We always return platlib here, even if it differs to purelib, because we can # always install pure-Python code into the platlib safely too. It's also very # unlikely to differ for a venv. return self.get_paths()["platlib"]
@property def working_set(self): ifnot self.exists: raise ValueError("trying to read working_set when venv doesn't exist")
if self._working_set isNone:
self._working_set = get_pkg_resources().WorkingSet((self.lib_path,))
return self._working_set
def activate(self):
paths = self.get_paths()
# Setup the path and site packages as if we'd launched with the virtualenv active
bin_dir = paths["scripts"]
os.environ["PATH"] = os.pathsep.join([bin_dir] + os.environ.get("PATH", "").split(os.pathsep))
# While not required (`./venv/bin/python3` won't set it, but # `source ./venv/bin/activate && python3` will), we have historically set this.
os.environ["VIRTUAL_ENV"] = self.path
prev_length = len(sys.path)
# Add the venv library paths as sitedirs. for key in ["purelib", "platlib"]:
site.addsitedir(paths[key])
# Rearrange the path
sys.path[:] = sys.path[prev_length:] + sys.path[0:prev_length]
# Change prefixes, similar to what initconfig/site does for venvs.
sys.exec_prefix = self.path
sys.prefix = self.path
def start(self): ifnot self.exists or self.broken_link:
self.create()
self.activate()
# `--prefer-binary` guards against race conditions when installation # occurs while packages are in the process of being published.
call(self.pip_path, "--disable-pip-version-check", "install", "--prefer-binary", *requirements)
def install_requirements(self, *requirements_paths):
install = [] # Check which requirements are already satisfied, to skip calling pip # at all in the case that we've already installed everything, and to # minimise the installs in other cases. for requirements_path in requirements_paths: with open(requirements_path) as f: try:
self.working_set.require(f.read()) except Exception:
install.append(requirements_path)
if install: # `--prefer-binary` guards against race conditions when installation # occurs while packages are in the process of being published.
cmd = [self.pip_path, "--disable-pip-version-check", "install", "--prefer-binary"] for path in install:
cmd.extend(["-r", path])
call(*cmd)
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.