From 76a39c76f50b20a957a932c9a929556ff75eb6e5 Mon Sep 17 00:00:00 2001 From: Chan Kang Date: Fri, 10 May 2024 02:41:32 -0400 Subject: [PATCH] add `sys_path` to `Interpreter` struct (#3500) ## Summary likely necessary to resolve https://github.com/astral-sh/uv/issues/2500 made this a separate PR in an attempt to make the changes as small as possible; let me know if it's preferred to keep them as a single PR. ## Test Plan - edited the test in `interpreter.rs` - tested manually via `println!` ``` $ cargo run --quiet pip show test ["/Users/chankang/Library/Caches/uv/.tmpKzNEPN", "/Users/chankang/.pyenv/versions/3.12.2/lib/python312.zip", "/Users/chankang/.pyenv/versions/3.12.2/lib/python3.12", "/Users/chankang/.pyenv/versions/3.12.2/lib/python3.12/lib-dynload", "/Users/chankang/repos/uv/.venv/lib/python3.12/site-packages"] warning: Package(s) not found for: test chankang@chans-Air ~/repos/uv - (syspath) $ git diff diff --git a/crates/uv-interpreter/src/environment.rs b/crates/uv-interpreter/src/environment.rs index 33b785ce..8ebf0864 100644 --- a/crates/uv-interpreter/src/environment.rs +++ b/crates/uv-interpreter/src/environment.rs @@ -106,6 +106,7 @@ impl PythonEnvironment { /// Some distributions also create symbolic links from `purelib` to `platlib`; in such cases, we /// still deduplicate the entries, returning a single path. pub fn site_packages(&self) -> impl Iterator { + println!("{:?}", self.interpreter.sys_path()); if let Some(target) = self.interpreter.target() { Either::Left(std::iter::once(target.root())) } else { chankang@chans-Air ~/repos/uv - (syspath) $ python -c "import sys; print(sys.path)" ['', '/Users/chankang/.pyenv/versions/3.12.2/lib/python312.zip', '/Users/chankang/.pyenv/versions/3.12.2/lib/python3.12', '/Users/chankang/.pyenv/versions/3.12.2/lib/python3.12/lib-dynload', '/Users/chankang/.pyenv/versions/3.12.2/lib/python3.12/site-packages'] chankang@chans-Air ~/repos/uv - (syspath) ``` --- .../uv-interpreter/python/get_interpreter_info.py | 1 + crates/uv-interpreter/src/interpreter.rs | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/crates/uv-interpreter/python/get_interpreter_info.py b/crates/uv-interpreter/python/get_interpreter_info.py index c8e83ecdc..46045d446 100644 --- a/crates/uv-interpreter/python/get_interpreter_info.py +++ b/crates/uv-interpreter/python/get_interpreter_info.py @@ -550,6 +550,7 @@ def main() -> None: "prefix": sys.prefix, "base_executable": getattr(sys, "_base_executable", None), "sys_executable": sys.executable, + "sys_path": sys.path, "stdlib": sysconfig.get_path("stdlib"), "scheme": get_scheme(), "virtualenv": get_virtualenv(), diff --git a/crates/uv-interpreter/src/interpreter.rs b/crates/uv-interpreter/src/interpreter.rs index 2ed99ce6f..1331d60b1 100644 --- a/crates/uv-interpreter/src/interpreter.rs +++ b/crates/uv-interpreter/src/interpreter.rs @@ -31,6 +31,7 @@ pub struct Interpreter { base_prefix: PathBuf, base_executable: Option, sys_executable: PathBuf, + sys_path: Vec, stdlib: PathBuf, tags: OnceCell, target: Option, @@ -59,6 +60,7 @@ impl Interpreter { base_prefix: info.base_prefix, base_executable: info.base_executable, sys_executable: info.sys_executable, + sys_path: info.sys_path, stdlib: info.stdlib, tags: OnceCell::new(), target: None, @@ -89,6 +91,7 @@ impl Interpreter { base_prefix: PathBuf::from("/dev/null"), base_executable: None, sys_executable: PathBuf::from("/dev/null"), + sys_path: vec![], stdlib: PathBuf::from("/dev/null"), tags: OnceCell::new(), target: None, @@ -283,6 +286,11 @@ impl Interpreter { &self.sys_executable } + /// Return the `sys.path` for this Python interpreter. + pub fn sys_path(&self) -> &Vec { + &self.sys_path + } + /// Return the `stdlib` path for this Python interpreter, as returned by `sysconfig.get_paths()`. pub fn stdlib(&self) -> &Path { &self.stdlib @@ -419,6 +427,7 @@ struct InterpreterInfo { base_prefix: PathBuf, base_executable: Option, sys_executable: PathBuf, + sys_path: Vec, stdlib: PathBuf, gil_disabled: bool, } @@ -634,6 +643,10 @@ mod tests { "base_prefix": "/home/ferris/.pyenv/versions/3.12.0", "prefix": "/home/ferris/projects/uv/.venv", "sys_executable": "/home/ferris/projects/uv/.venv/bin/python", + "sys_path": [ + "/home/ferris/.pyenv/versions/3.12.0/lib/python3.12/lib/python3.12", + "/home/ferris/.pyenv/versions/3.12.0/lib/python3.12/site-packages" + ], "stdlib": "/home/ferris/.pyenv/versions/3.12.0/lib/python3.12", "scheme": { "data": "/home/ferris/.pyenv/versions/3.12.0",