Skip to content

Commit

Permalink
Only query interpreter for system_site_packages = true virtualenv
Browse files Browse the repository at this point in the history
  • Loading branch information
pradyunsg committed Jan 13, 2025
1 parent 374a63a commit 56e9520
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
8 changes: 7 additions & 1 deletion crates/uv-python/python/get_interpreter_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,11 @@ def get_operating_system_and_architecture():


def main() -> None:
sys_path_before_site = [os.path.abspath(p) for p in sys.path]
import site
site.main()
sys_path = sys.path[:]

markers = {
"implementation_name": implementation_name,
"implementation_version": implementation_version,
Expand Down Expand Up @@ -579,7 +584,8 @@ def main() -> None:
"sys_prefix": sys.prefix,
"sys_base_executable": getattr(sys, "_base_executable", None),
"sys_executable": sys.executable,
"sys_path": sys.path,
"sys_path_before_site": sys_path_before_site,
"sys_path": sys_path,
"stdlib": sysconfig.get_path("stdlib"),
# Prior to the introduction of `sysconfig` patching, python-build-standalone installations would always use
# "/install" as the prefix. With `sysconfig` patching, we rewrite the prefix to match the actual installation
Expand Down
11 changes: 11 additions & 0 deletions crates/uv-python/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct Interpreter {
sys_base_prefix: PathBuf,
sys_base_executable: Option<PathBuf>,
sys_executable: PathBuf,
sys_path_before_site: Vec<PathBuf>,
sys_path: Vec<PathBuf>,
stdlib: PathBuf,
standalone: bool,
Expand Down Expand Up @@ -78,6 +79,7 @@ impl Interpreter {
sys_base_prefix: info.sys_base_prefix,
sys_base_executable: info.sys_base_executable,
sys_executable: info.sys_executable,
sys_path_before_site: info.sys_path_before_site,
sys_path: info.sys_path,
stdlib: info.stdlib,
standalone: info.standalone,
Expand All @@ -90,10 +92,17 @@ impl Interpreter {
/// Return a new [`Interpreter`] with the given virtual environment root.
#[must_use]
pub fn with_virtualenv(self, virtualenv: VirtualEnvironment) -> Self {
let sys_path: Vec<PathBuf> = self
.sys_path_before_site
.iter()
.cloned()
.chain(self.site_packages().map(Cow::into_owned))
.collect();
Self {
scheme: virtualenv.scheme,
sys_executable: virtualenv.executable,
sys_prefix: virtualenv.root,
sys_path,
target: None,
prefix: None,
..self
Expand Down Expand Up @@ -618,6 +627,7 @@ struct InterpreterInfo {
sys_base_prefix: PathBuf,
sys_base_executable: Option<PathBuf>,
sys_executable: PathBuf,
sys_path_before_site: Vec<PathBuf>,
sys_path: Vec<PathBuf>,
stdlib: PathBuf,
standalone: bool,
Expand All @@ -640,6 +650,7 @@ impl InterpreterInfo {
);
let output = Command::new(interpreter)
.arg("-I") // Isolated mode.
.arg("-S") // Don't import site for initialization.
.arg("-B") // Don't write bytecode.
.arg("-c")
.arg(script)
Expand Down
6 changes: 5 additions & 1 deletion crates/uv-virtualenv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ pub fn create_venv(
)?;

// Create the corresponding `PythonEnvironment`.
let interpreter = Interpreter::query(virtualenv.executable, cache)?;
let interpreter = if system_site_packages {
Interpreter::query(virtualenv.executable, cache)?
} else {
interpreter.with_virtualenv(virtualenv)
};
Ok(PythonEnvironment::from_interpreter(interpreter))
}

0 comments on commit 56e9520

Please sign in to comment.