在Python中,我可以执行os.system("pip install bs4")
。 Rust中有等效的东西吗?我见过std::process::Command
,但每次似乎都失败了:
use std::process::Command;
Command::new("pip")
.arg("install")
.arg("bs4")
.spawn()
.expect("pip failed");
有什么方法可以让代码执行真正的shell并在终端中运行它们?
以下内容对我有用:
use std::process::Command;
Command::new("pip")
.args(&["install", "bs4"])
.spawn()
.expect("failed to execute process");
使用它来分析故障:
use std::process::Command;
let output = Command::new("pip")
.args(&["install", "bs4"])
.output()
.expect("failed to execute process");
println!("status: {}", output.status);
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
Pip需要root权限,所以请确保以足够的特权运行您的二进制文件。