From 6a7954cdd07bc21adea11423b055175fe29758de Mon Sep 17 00:00:00 2001 From: konsti Date: Thu, 12 Oct 2023 20:41:52 +0200 Subject: [PATCH] Add `-p` base python option to venv command (#92) This is the same option that `virtualenv` offers, except that we only support absolute paths atm and not e.g. `-p 3.10` (which we need to eventually). --- crates/puffin-cli/src/commands/venv.rs | 18 +++++++++++++----- crates/puffin-cli/src/main.rs | 7 ++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/crates/puffin-cli/src/commands/venv.rs b/crates/puffin-cli/src/commands/venv.rs index f05107d5c..f3f6ca61a 100644 --- a/crates/puffin-cli/src/commands/venv.rs +++ b/crates/puffin-cli/src/commands/venv.rs @@ -8,15 +8,23 @@ use crate::commands::ExitStatus; use crate::printer::Printer; /// Create a virtual environment. -pub(crate) async fn venv(path: &Path, mut printer: Printer) -> Result { +pub(crate) async fn venv( + path: &Path, + base_python: Option<&Path>, + mut printer: Printer, +) -> Result { // Locate the Python interpreter. // TODO(charlie): Look at how Maturin discovers and ranks all the available Python interpreters. - let executable = which::which("python3").or_else(|_| which::which("python"))?; - let interpreter_info = gourgeist::get_interpreter_info(&executable)?; + let base_python = if let Some(base_python) = base_python { + base_python.to_path_buf() + } else { + which::which("python3").or_else(|_| which::which("python"))? + }; + let interpreter_info = gourgeist::get_interpreter_info(&base_python)?; writeln!( printer, "Using Python interpreter: {}", - format!("{}", executable.display()).cyan() + format!("{}", base_python.display()).cyan() )?; // If the path already exists, remove it. @@ -30,7 +38,7 @@ pub(crate) async fn venv(path: &Path, mut printer: Printer) -> Result, /// The path to the virtual environment to create. name: PathBuf, } @@ -143,7 +148,7 @@ async fn main() -> ExitCode { ) .await } - Commands::Venv(args) => commands::venv(&args.name, printer).await, + Commands::Venv(args) => commands::venv(&args.name, args.python.as_deref(), printer).await, }; match result {