Skip to content

Commit

Permalink
fix: add missing files for windows when creating venv (#148)
Browse files Browse the repository at this point in the history
With this fix we will move python.exe / pythonw.exe from Lib/Scripts/nt when running on windows.
  • Loading branch information
nichmor authored Jan 18, 2024
1 parent 8cd9966 commit acf2dbe
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 15 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/rust-compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,35 @@ jobs:
${{ steps.test-options.outputs.CARGO_TEST_OPTIONS}}
--
--nocapture

# test if venv works properly on windows
# using old and newest python version
# our implementation will fail on 3.7.4
# and we emit warning
- name: Install pixi
uses: prefix-dev/[email protected]
with:
run-install: false

- name: Run pixi python==3.7.5 test
if: contains(matrix.name, 'Windows')
shell: bash
run: |
mkdir pixi_old
cd pixi_old
pixi init
pixi add "python==3.7.5"
cd ../
cargo run -- boltons --only-sdists -p /d/a/rip/rip/pixi_old/.pixi/env/python.exe
- name: Run pixi latest python test
if: contains(matrix.name, 'Windows')
shell: bash
run: |
mkdir pixi_new
cd pixi_new
pixi init
pixi add python
cd ../
cargo run -- boltons --only-sdists -p /d/a/rip/rip/pixi_new/.pixi/env/python.exe
50 changes: 35 additions & 15 deletions crates/rattler_installs_packages/src/python_env/venv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,10 @@ impl VEnv {
let exe_path = install_paths.scripts().join(base_python_name);
let abs_exe_path = venv_abs_dir.join(exe_path);

#[cfg(not(windows))]
{
Self::setup_python(&abs_exe_path, &base_python_path, base_python_version)?;
}

#[cfg(windows)]
{
Self::setup_python(&abs_exe_path, &base_python_path)?;
}

Ok(VEnv::new(venv_abs_dir.to_path_buf(), install_paths))
}

Expand Down Expand Up @@ -245,18 +239,18 @@ prompt = {}"#,
pub fn setup_python(
venv_exe_path: &Path,
original_python_exe: &Path,
#[cfg(not(windows))] python_version: PythonInterpreterVersion,
python_version: PythonInterpreterVersion,
) -> std::io::Result<()> {
if !venv_exe_path.exists() {
copy_file(original_python_exe, venv_exe_path)?;
}

let venv_bin = venv_exe_path
.parent()
.expect("venv exe binary should have parent folder");

#[cfg(not(windows))]
{
if !venv_exe_path.exists() {
copy_file(original_python_exe, venv_exe_path)?;
}

let python_bins = [
"python",
"python3",
Expand All @@ -273,6 +267,11 @@ prompt = {}"#,

#[cfg(windows)]
{
if python_version.major <= 3 && python_version.minor <= 7 && python_version.patch <= 4 {
tracing::warn!(
"Creation of venv for <=3.7.4 on windows may fail. Please use newer version"
);
}
let base_exe_name = venv_exe_path
.file_name()
.expect("cannot get windows venv exe name");
Expand All @@ -291,11 +290,32 @@ prompt = {}"#,
.expect("cannot get system python parent folder");
for bin_name in python_bins.into_iter() {
let original_python_bin = original_python_bin_dir.join(bin_name);
let original_python_scripts = original_python_bin_dir
.join("Lib/venv/scripts/nt")
.join(bin_name);
let venv_python_bin = venv_bin.join(bin_name);

if original_python_bin.exists() {
let venv_python_bin = venv_bin.join(bin_name);
if !venv_python_bin.exists() {
copy_file(venv_exe_path, &venv_python_bin)?;
if original_python_bin.exists() && !venv_python_bin.exists() {
if original_python_scripts.is_file() {
copy_file(original_python_scripts, &venv_python_bin)?;
} else {
// If used python is built from source code
// or we are using a python from venv which
// was created using python from source code
// we need to move venvlauncher
// and venvwlauncher as python.exe and pythonw.exe
// instead of using the python.exe shipped with /venv/scripts/nt
let launcher_bin_name = if bin_name.contains("python") {
bin_name.replace("python", "venvlauncher")
} else if bin_name.contains("pythonw") {
bin_name.replace("pythonw", "venvwlauncher")
} else {
bin_name.to_owned()
};
let original_launcher_bin = original_python_bin_dir.join(launcher_bin_name);
if original_launcher_bin.exists() {
copy_file(original_launcher_bin, &venv_python_bin)?;
}
}
}
}
Expand Down

0 comments on commit acf2dbe

Please sign in to comment.