Retrieve all `sitepackages` from interpreters

This commit is contained in:
Zanie Blue 2025-08-08 10:02:30 -05:00
parent 57df0146e2
commit e1f3127184
3 changed files with 16 additions and 1 deletions

View File

@ -4,6 +4,7 @@ Queries information about the current Python interpreter and prints it as JSON.
The script will exit with status 0 on known error that are turned into rust errors.
"""
import site
import sys
import json
@ -637,6 +638,7 @@ def main() -> None:
# temporary path to `sys.path` so we can import it, which we have to strip later
# to avoid having this now-deleted path around.
"sys_path": sys.path[1:],
"site_packages": site.getsitepackages(),
"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

View File

@ -51,6 +51,7 @@ pub struct Interpreter {
sys_base_executable: Option<PathBuf>,
sys_executable: PathBuf,
sys_path: Vec<PathBuf>,
site_packages: Vec<PathBuf>,
stdlib: PathBuf,
standalone: bool,
tags: OnceLock<Tags>,
@ -86,6 +87,7 @@ impl Interpreter {
sys_base_executable: info.sys_base_executable,
sys_executable: info.sys_executable,
sys_path: info.sys_path,
site_packages: info.site_packages,
stdlib: info.stdlib,
standalone: info.standalone,
tags: OnceLock::new(),
@ -439,9 +441,13 @@ impl Interpreter {
}
/// Return the `sys.path` for this Python interpreter.
pub fn sys_path(&self) -> &Vec<PathBuf> {
pub fn sys_path(&self) -> &[PathBuf] {
&self.sys_path
}
/// Return the `site.getsitepackages` for this Python interpreter.
pub fn site_packages_(&self) -> &[PathBuf] {
&self.site_packages
}
/// Return the `stdlib` path for this Python interpreter, as returned by `sysconfig.get_paths()`.
pub fn stdlib(&self) -> &Path {
@ -870,6 +876,7 @@ struct InterpreterInfo {
sys_base_executable: Option<PathBuf>,
sys_executable: PathBuf,
sys_path: Vec<PathBuf>,
site_packages: Vec<PathBuf>,
stdlib: PathBuf,
standalone: bool,
pointer_size: PointerSize,
@ -1247,6 +1254,9 @@ mod tests {
"/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"
],
"site_packages": [
"/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",

View File

@ -272,6 +272,9 @@ mod tests {
"/home/ferris/.pyenv/versions/{FULL_VERSION}/lib/python{VERSION}/lib/python{VERSION}",
"/home/ferris/.pyenv/versions/{FULL_VERSION}/lib/python{VERSION}/site-packages"
],
"site_packages": [
"/home/ferris/.pyenv/versions/{FULL_VERSION}/lib/python{VERSION}/site-packages"
],
"stdlib": "/home/ferris/.pyenv/versions/{FULL_VERSION}/lib/python{VERSION}",
"scheme": {
"data": "/home/ferris/.pyenv/versions/{FULL_VERSION}",