Skip to content

Make :PymodeRun execute with the activated virtualenv interpreter#1200

Draft
Copilot wants to merge 5 commits into
developfrom
copilot/fix-venv-activation-issue
Draft

Make :PymodeRun execute with the activated virtualenv interpreter#1200
Copilot wants to merge 5 commits into
developfrom
copilot/fix-venv-activation-issue

Conversation

Copy link
Copy Markdown

Copilot AI commented Apr 21, 2026

`Pymode` correctly detected/activated virtualenvs, but `:PymodeRun` still executed code with Vim's embedded/system Python, causing version mismatch (e.g. active 3.13 venv, output from 3.14 system Python).
This change aligns run behavior with virtualenv activation so executed code uses the selected environment interpreter.

  • Run-path correction for virtualenv-enabled sessions

    • Updated `pymode/run.py` so `run_code()` checks `g:pymode_virtualenv_enabled` and resolves the venv Python executable (`bin/python` on Unix, `Scripts/python.exe` on Windows).
    • When available, code is executed via that interpreter through a temp script + subprocess, preserving cwd and capturing stdout/stderr for existing quickfix/output flow.
    • Keeps existing in-process execution path as fallback when no active virtualenv interpreter is available.
  • Correctness fixes

    • `file` is now set to the real source file path inside the subprocess (previously resolved to the temp file path, breaking code that uses `file` at runtime).
    • Subprocess tracebacks have the temp file path replaced with the real source path so error messages point to the correct file.
    • Explicit UTF-8 encoding passed to `subprocess.Popen`.
    • Added `g:pymode_run_timeout` (default `0` = no limit) to prevent Vim from hanging if a script never terminates; `TimeoutExpired` is caught, the process is killed, and an error message is appended.
    • Pre-existing bug fixed: `sys.stdout`/`sys.stderr` are now restored before returning early on a non-zero `SystemExit`.
  • Regression coverage

    • Added a focused Vader test in `tests/vader/commands.vader` that:
      • creates a temporary virtualenv,
      • activates it with `:PymodeVirtualenv`,
      • runs `:PymodeRun`,
      • asserts `run` output includes the venv interpreter path.
    • Fixed `string(venv_dir)` → `fnameescape(venv_dir)` in the test (`string()` wraps the path in quotes, producing a malformed argument).
    • Test now saves and restores `g:pymode_virtualenv_enabled` / `g:pymode_virtualenv` so it does not pollute subsequent tests.
    • Cleanup (`delete`) and state restore run unconditionally regardless of whether venv creation succeeded.
  • Docs update

    • Clarified in `doc/pymode.txt` that `:PymodeRun` uses the virtualenv Python executable when a virtualenv is activated.
    • Added `g:pymode_run_timeout` default in `plugin/pymode.vim`.

```python

before: often printed system interpreter from Vim host runtime

import sys
print(sys.executable)

after: with :PymodeVirtualenv .venv, prints .../.venv/bin/python (or Scripts/python.exe)

```

Copilot AI linked an issue Apr 21, 2026 that may be closed by this pull request
@github-actions
Copy link
Copy Markdown

🧪 Test Results Summary

This comment will be updated automatically as tests complete.

Python linux-3.10 ✅

  • Status: PASSED
  • Python Version: 3.10.20
  • Vim Version: 9.1
  • Tests: 9/9 passed
  • Assertions: 99/107 passed

Python linux-3.11 ✅

  • Status: PASSED
  • Python Version: 3.11.15
  • Vim Version: 9.1
  • Tests: 9/9 passed
  • Assertions: 99/107 passed

Python linux-3.12 ✅

  • Status: PASSED
  • Python Version: 3.12.13
  • Vim Version: 9.1
  • Tests: 9/9 passed
  • Assertions: 99/107 passed

Python linux-3.13 ✅

  • Status: PASSED
  • Python Version: 3.13.13
  • Vim Version: 9.1
  • Tests: 9/9 passed
  • Assertions: 99/107 passed

Python linux-3.14 ✅

  • Status: PASSED
  • Python Version: 3.14.4
  • Vim Version: 9.1
  • Tests: 9/9 passed
  • Assertions: 99/107 passed

Python macos-3.10 ✅

  • Status: PASSED
  • Python Version: 3.14.4
  • Vim Version: 9.2
  • Tests: 9/9 passed
  • Assertions: 99/107 passed

Python macos-3.11 ✅

  • Status: PASSED
  • Python Version: 3.14.4
  • Vim Version: 9.2
  • Tests: 9/9 passed
  • Assertions: 99/107 passed

Python macos-3.12 ✅

  • Status: PASSED
  • Python Version: 3.14.4
  • Vim Version: 9.2
  • Tests: 9/9 passed
  • Assertions: 99/107 passed

Python macos-3.13 ✅

  • Status: PASSED
  • Python Version: 3.14.4
  • Vim Version: 9.2
  • Tests: 9/9 passed
  • Assertions: 99/107 passed

Python windows-3.10 ✅

  • Status: PASSED
  • Python Version: 3.10.11
  • Vim Version: ed
  • Tests: 9/9 passed
  • Assertions: 99/107 passed

Python windows-3.11 ✅

  • Status: PASSED
  • Python Version: 3.11.9
  • Vim Version: ed
  • Tests: 9/9 passed
  • Assertions: 99/107 passed

Python windows-3.12 ✅

  • Status: PASSED
  • Python Version: 3.12.10
  • Vim Version: ed
  • Tests: 9/9 passed
  • Assertions: 99/107 passed

Python windows-3.13 ✅

  • Status: PASSED
  • Python Version: 3.13.13
  • Vim Version: ed
  • Tests: 9/9 passed
  • Assertions: 99/107 passed

📊 Overall Summary

  • Python Versions Tested: 13
  • Total Tests: 117
  • Passed: 117
  • Failed: 0
  • Total Assertions: 1391
  • Passed Assertions: 1287

🎉 All tests passed across all Python versions!


Generated automatically by CI/CD workflow

Copilot AI changed the title [WIP] Fix virtual environment activation issue in Pymode Make :PymodeRun execute with the activated virtualenv interpreter Apr 21, 2026
Copilot AI requested a review from diraol April 21, 2026 20:48
- Set __file__ correctly in subprocess so user code referencing it sees
  the real source path instead of a temp file path
- Rewrite temp file paths in subprocess tracebacks to the real source file
- Add explicit UTF-8 encoding to subprocess Popen call
- Add g:pymode_run_timeout support with TimeoutExpired handling to
  prevent Vim from hanging on non-terminating scripts
- Restore sys.stdout/sys.stderr before returning on SystemExit with
  non-false exit code (pre-existing leak)
- Fix test: use fnameescape() instead of string() for PymodeVirtualenv
  argument (string() wraps in quotes, producing a malformed path)
- Save and restore g:pymode_virtualenv_enabled/g:pymode_virtualenv in
  the test to avoid polluting subsequent tests
- Remove misleading Assert 1 "skip" patterns; drop unreachable else
  branches so cleanup runs unconditionally

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

trouble with venv

2 participants