Python Virtualenv Debugging Guide
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
| Step | What to confirm |
|---|---|
| Identify the executable | The Python binary decides which packages and paths are visible. |
| Use python -m pip | Install into the interpreter you are actually using. |
| Compare shell and service | A 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 pythonReview checklist
- Check virtualenv activation and prompt.
- Print sys.executable inside the failing process.
- Keep requirements and lock files aligned.
- Check service user permissions.
- 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.