mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-27 19:59:07 +00:00
feat: search site-packages
with pyimport
This commit is contained in:
parent
658ed24482
commit
bb17537178
5 changed files with 158 additions and 25 deletions
|
@ -1,6 +1,7 @@
|
|||
//! utilities for calling CPython.
|
||||
//!
|
||||
//! CPythonを呼び出すためのユーティリティー
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
|
||||
use crate::fn_name_full;
|
||||
|
@ -687,6 +688,32 @@ pub fn env_python_version() -> PythonVersion {
|
|||
get_python_version(&which_python())
|
||||
}
|
||||
|
||||
pub fn get_sys_path() -> Vec<PathBuf> {
|
||||
let py_command = which_python();
|
||||
let code = "import sys; print('\\n'.join(sys.path))";
|
||||
let out = if cfg!(windows) {
|
||||
Command::new("cmd")
|
||||
.arg("/C")
|
||||
.arg(py_command)
|
||||
.arg("-c")
|
||||
.arg(code)
|
||||
.output()
|
||||
.expect("cannot get the sys.path")
|
||||
} else {
|
||||
let exec_command = format!("{py_command} -c \"{code}\"");
|
||||
Command::new("sh")
|
||||
.arg("-c")
|
||||
.arg(exec_command)
|
||||
.output()
|
||||
.expect("cannot get the sys.path")
|
||||
};
|
||||
let s_sys_path = String::from_utf8(out.stdout).unwrap();
|
||||
s_sys_path
|
||||
.split('\n')
|
||||
.map(|s| PathBuf::from(s.trim().to_string()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// executes over a shell, cause `python` may not exist as an executable file (like pyenv)
|
||||
pub fn exec_pyc<S: Into<String>>(
|
||||
file: S,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue