-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can expectrl spawn with modified environment variables? #69
Comments
Hi @Centauria Something like this? use expectrl::{Expect, Regex};
fn main() {
let path_to_python_bin = "/usr/bin/python";
let mut p = expectrl::spawn(path_to_python_bin).unwrap();
p.expect(Regex("\n")).unwrap(); // skip python prompt
p.send_line("print('Does it work?')").unwrap();
let mut buf = String::new();
p.read_line(&mut buf).unwrap();
assert_eq!(buf, ">>> Does it work?\r\n");
} Or like this fn main() {
let path_to_python_bin = "/usr/bin/python";
let p = expectrl::spawn(path_to_python_bin).unwrap();
let mut p = expectrl::repl::ReplSession::new(p, ">>> ");
p.set_quit_command("quit()");
p.expect_prompt().unwrap();
p.send_line("print('Does it work?')").unwrap();
let mut buf = String::new();
p.read_line(&mut buf).unwrap();
assert_eq!(buf, "Does it work?\r\n");
} |
Thank you for the fast reply. But what should I do if I need the spawned python recognize some specific environment variables, such as "PYTHONPATH", for further usage? |
Well the example you've provided use std::env;
use std::path::PathBuf;
fn main() {
set_env("SOMEWHERE_NEW");
let p = expectrl::spawn("python").unwrap();
let mut p = expectrl::repl::ReplSession::new(p, ">>> ");
p.set_quit_command("quit()");
p.expect_prompt().unwrap();
let output = p.execute("import os; print(os.environ.get('PATH'))").unwrap();
println!("{}", String::from_utf8_lossy(&output));
}
fn set_env(p: &str) {
if let Some(path) = env::var_os("PATH") {
let mut paths = env::split_paths(&path).collect::<Vec<_>>();
paths.push(PathBuf::from(p));
let new_path = env::join_paths(paths).unwrap();
env::set_var("PATH", new_path);
}
} You could also use use std::process::Command;
use expectrl::Session;
fn main() {
let mut cmd = Command::new("python");
cmd.env("SOME_NEW_ENV", "NEW VALUE");
let p = Session::spawn(cmd).unwrap();
let mut p = expectrl::repl::ReplSession::new(p, ">>> ");
p.set_quit_command("quit()");
p.expect_prompt().unwrap();
let output = p
.execute("import os; print(os.environ.get('SOME_NEW_ENV'))")
.unwrap();
println!("{}", String::from_utf8_lossy(&output));
} |
The first code snippet do not work. The second code snippet do not work. I changed it like below: use std::process::Command;
use expectrl::repl::ReplSession;
use expectrl::Session;
fn main() {
let mut cmd = Command::new(r"D:\path\to\anaconda3\python.exe");
cmd.env("SOME_NEW_ENV", "NEW VALUE");
let p = Session::spawn(cmd).unwrap();
let mut p = ReplSession::new(
p,
">>> ".to_owned(),
Some("import sys; sys.exit()".to_owned()),
false,
);
p.expect_prompt().unwrap();
let output = p
.execute("import os; print(os.environ.get('SOME_NEW_ENV'))")
.unwrap();
println!("{}", String::from_utf8_lossy(&output));
} this code do not work on Windows.
I changed On Windows 10, 19045.4651, it prints a blank line
on Windows 11, 22631.3880, it prints
|
Let's say this code
I need it find
python
in somewhere else beyond system PATH,so I add something like
before that.
But this won't work.
So is there an approach for doing this?
The text was updated successfully, but these errors were encountered: