1

In Python, I could do os.system("pip install bs4"). Is there any equivalent in Rust? I've seen std::process::Command, but this seems to fail each time:

use std::process::Command;
Command::new("pip")
    .arg("install")
    .arg("bs4")
    .spawn()
    .expect("pip failed");

Is there any way to have the code execute a real shell and have them run in the terminal?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366

1 Answers1

0

Pip requires root permissions so be sure to run your binary with sufficient privileges.

The following worked for me:

use std::process::Command;
Command::new("pip")
    .args(&["install", "bs4"])
    .spawn()
    .expect("failed to execute process");

Use this to analyze the failure:

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));

Example was derived from here:

How do I invoke a system command in Rust and capture its output?

Iron Oxidizer
  • 31
  • 1
  • 1