Skip to content
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

fix: properly perform venv activation on windows #26038

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions influxdb3/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@ fn main() -> Result<(), std::io::Error> {
#[cfg(feature = "system-py")]
set_pythonhome();

#[cfg(all(target_os = "windows", feature = "system-py"))]
set_pythonpath();

// load all environment variables from .env before doing anything
load_dotenv();

Expand Down Expand Up @@ -344,27 +341,3 @@ fn set_pythonhome() {
}
};
}

// XXX: this should be somewhere more appropriate
#[cfg(target_os = "windows")]
fn set_pythonpath() {
let exe_path = env::current_exe().unwrap();
let exe_dir = exe_path.parent().unwrap();
let pythonpath = exe_dir.join("python/Lib");

// This shouldn't be needed, but it is on Windows
match env::var("PYTHONPATH") {
Ok(v) => {
let new_path = format!("{};{}", pythonpath.display(), v);
unsafe { env::set_var("PYTHONPATH", &new_path) };
//println!("Updated PYTHONPATH to: {}", env::var("PYTHONPATH").unwrap());
}
Err(env::VarError::NotPresent) => {
unsafe { env::set_var("PYTHONPATH", &pythonpath) };
//println!("Updated PYTHONPATH to: {}", env::var("PYTHONPATH").unwrap());
}
Err(e) => {
eprintln!("Failed to retrieve PYTHONPATH: {e}");
}
}
}
24 changes: 15 additions & 9 deletions influxdb3_processing_engine/src/virtualenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub enum VenvError {
}

fn get_python_version() -> Result<(u8, u8), std::io::Error> {
// XXX: put this somewhere common
let python_exe_bn = if cfg!(windows) {
"python.exe"
} else {
Expand Down Expand Up @@ -54,10 +53,14 @@ fn get_python_version() -> Result<(u8, u8), std::io::Error> {

fn set_pythonpath(venv_dir: &Path) -> Result<(), std::io::Error> {
let (major, minor) = get_python_version()?;
let site_packages = venv_dir
.join("lib")
.join(format!("python{}.{}", major, minor))
.join("site-packages");
let site_packages = if cfg!(target_os = "windows") {
venv_dir.join("Lib").join("site-packages")
} else {
venv_dir
.join("lib")
.join(format!("python{}.{}", major, minor))
.join("site-packages")
};

debug!("Setting PYTHONPATH to: {}", site_packages.to_string_lossy());
std::env::set_var("PYTHONPATH", &site_packages);
Expand All @@ -83,13 +86,11 @@ pub fn init_pyo3() {
});
}

// FIXME: this still doesn't work right on windows (sys.path isn't adding the
// venv's site-packages). Perhaps look at /path/to/venv/pyvenv.cfg?
pub(crate) fn initialize_venv(venv_path: &Path) -> Result<(), VenvError> {
use std::process::Command;

let activate_script = if cfg!(target_os = "windows") {
venv_path.join("Scripts").join("activate")
venv_path.join("Scripts").join("activate.bat")
} else {
venv_path.join("bin").join("activate")
};
Expand All @@ -101,9 +102,14 @@ pub(crate) fn initialize_venv(venv_path: &Path) -> Result<(), VenvError> {
)));
}

// Calling the activate script isn't enough to change our process' environment. Instead,
// source/call the script, print the resulting environment, capture its output and
// set all env vars found. This should future-proof us against changes to activate script
// specifics.
let output = if cfg!(target_os = "windows") {
Command::new("cmd")
.args(["/C", activate_script.to_str().unwrap()])
.arg("/C")
.arg(format!("{} && set", activate_script.to_str().unwrap()))
.output()?
} else {
Command::new("bash")
Expand Down