Python Virtualenv Debugging Guide

Find why Python works in one shell but fails in the service. Last updated September 1, 2026.

Debug Python virtual environments, pip paths, package imports, service users and deployment differences. This Formalint reference is written for working developers, DBAs and support engineers who need a repeatable debugging path instead of a one-line snippet with no context.

Use the notes below as a practical review order: understand the input, capture evidence, make one small change and verify the result before moving to the next assumption.

When to use this page

Find why Python works in one shell but fails in the service. It is most useful when a small validation or debugging mistake can create noisy tickets, misleading logs or hard-to-review production changes.

Practical workflow

StepWhat to confirm
Identify the executableThe Python binary decides which packages and paths are visible.
Use python -m pipInstall into the interpreter you are actually using.
Compare shell and serviceA systemd, Docker or web worker process may use a different environment.

Python environment checks

python --version
python -m pip --version
python -c "import sys; print(sys.executable); print(sys.path)"
where python
# Linux/macOS:
which python

Review checklist

  1. Check virtualenv activation and prompt.
  2. Print sys.executable inside the failing process.
  3. Keep requirements and lock files aligned.
  4. Check service user permissions.
  5. Avoid global pip installs as a debugging shortcut.

Common mistake

Most virtualenv bugs are not about Python syntax. They are about which Python is running.

Formalint is strongest when the page helps the developer decide what the tool cannot prove. Treat every formatter, regex and command as one layer of evidence, not the whole truth.

Frequently asked questions

Why does import work locally but fail in production?

The package may be installed in a different environment or missing from the service image.

Should I activate venv in scripts?

For repeatable operations, call the virtualenv Python path or document activation explicitly.

Related Formalint references

Continue with Python Runtime Guide, Python Indentation Fixer, Docker Container Logs Guide.