我的环境
~/.pyenv/shims/python)问题
我是一个生锈语言的初学者,并试图用matplotrust机箱制作情节。我写的代码如下,
extern crate matplotrust;
use matplotrust::{line_plot, Figure};
fn main() {
// load values in text file
let result = std::fs::read_to_string("test.txt");
let content = match result {
Ok(content) => content,
Err(error) => {
panic!("Could not read file: {}", error);
}
};
let lines_array: Vec<&str> = content.trim().split("\n").collect();
// insert values in vectors x and y
let mut x: Vec<f32> = Vec::new();
let mut y: Vec<f32> = Vec::new();
for line in lines_array {
let pair: Vec<&str> = line.trim().split(" ").collect();
x.push(pair[0].parse().unwrap());
y.push(pair[1].parse().unwrap());
}
// plot y against x
let lp = line_plot::<f32, f32>(x, y, None);
let mut figure = Figure::new();
figure.add_plot(lp.clone());
figure.add_plot(lp.clone());
print!("{:?}", figure.save("./test.png", None));
}我可以构建这段代码,但是在运行时会发生错误。
thread 'main' panicked at 'python binary not found at /usr/local/bin/python3: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/libcore/result.rs:999:5
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.它说python路径是错误的。但是在我的例子中,我使用了pyenv,并且没有在这个目录中安装python3。
如何解决这个问题?
我试过的
我试着用python3在/usr/local/bin中安装apt install,但是shell说python3已经安装在pyenv中了.
发布于 2019-08-23 09:55:27
@Jmb的评论解决了用户的问题。写一个答案只是为了说明在这种情况下该如何进行。
matplotlib锈蚀箱文档并不是很好(没有)。在本例中,您必须检查源代码的Figure.save()方法:
pub fn save(&mut self, output: &str, path: Option<&str>) -> (String, String, String) {
self.script += &format!("plt.savefig('{}')\n", output);
// create a temporary file
let mut tmpfile: NamedTempFile = tempfile::NamedTempFile::new().unwrap();
tmpfile.write_all(self.script.as_bytes());
let python_path = match path {
Some(s) => s,
None => "/usr/local/bin/python3"
};
fs::metadata(python_path).expect("python binary not found at /usr/local/bin/python3");
let mut echo_hello = Command::new(python_path);
echo_hello.arg(tmpfile.path());
echo_hello.output().expect("failed to execute process");
return (self.script.to_string(), output.to_string(), tmpfile.path().to_str().unwrap().to_string());
}重要的是这一条:
let python_path = match path {
Some(s) => s,
None => "/usr/local/bin/python3"
};你打电话给figure.save("./test.png", None) -> /usr/local/bin/python3。正如@Jmb所指出的,您可以通过figure.save("./test.png", Some("/path/to/python3"))指定Python3解释器路径以使其工作。
https://stackoverflow.com/questions/57612276
复制相似问题