Managing Python versions and dependencies often feels like an "alphabet soup" of tools. Here is how to organize pyenv, pyenv-virtualenv, and pipenv without them clashing.
The Toolset
- Pyenv → Manages Python Versions. It lets you switch between Python 3.8, 3.11, or 3.12 globally or per folder.
- Pyenv-virtualenv → An extension for Pyenv that manages Named Virtual Environments.
-
Pipenv → A Higher-level Manager. It handles both the virtual environment and package dependencies using a
Pipfile(replacingrequirements.txt).
Recommended Workflows
Option 1: The "Manual" Approach (Pyenv + Pyenv-Virtualenv)
Best for: Quick scripts, simple tools, or developers who prefer full control over pip.
- Install Python:
pyenv install 3.11.5 - Create Environment:
pyenv virtualenv 3.11.5 my-project-env - Local Link:
pyenv local my-project-env-
Result: Every time you enter this folder, your terminal automatically switches to
my-project-env.
-
Result: Every time you enter this folder, your terminal automatically switches to
- Manage Deps: Use
pip installand manually maintain arequirements.txt.
Option 2: The "Modern" Approach (Pyenv + Pipenv)
Best for: Production applications and collaborative projects where dependency locking is vital.
- Install Python:
pyenv install 3.11.5 - Initialize Pipenv:
pipenv install --python 3.11.5-
Note: You do not need to run
pyenv localfirst. Pipenv will communicate with Pyenv to find the correct version.
-
Note: You do not need to run
- Activate:
pipenv shell - Manage Deps: Use
pipenv install <package>. This automatically updates yourPipfileandPipfile.lock.
Critical Disclaimers
[!CAUTION]
Avoid "Nesting" Environments: Never runpipenv installwhile apyenv-virtualenvis already active. This can cause Pipenv to install packages into the wrong environment or create a "ghost" environment that is difficult to debug. Pick one manager per project.[!IMPORTANT]
Storage Locations: > *pyenv-virtualenvstores environments in~/.pyenv/versions/.
pipenvstores them in a hidden central folder (usually~/.local/share/virtualenvs/).- Pro Tip: To keep Pipenv environments inside your project folder (like
.venv), setexport PIPENV_VENV_IN_PROJECT=1in your shell profile.
Comparison at a Glance
| Feature | Pyenv-Virtualenv | Pipenv |
|---|---|---|
| Primary Goal | Isolation | Dependency Management |
| Tracking | None (Manual requirements.txt) |
Deterministic (Pipfile.lock) |
| Activation | Automatic via .python-version
|
Manual via pipenv shell
|
| Complexity | Low | Medium |
Final Rule of Thumb
- Use Pyenv + Pipenv if you need a "lockfile" to ensure your teammates have the exact same package versions as you.
- Use Pyenv + Pyenv-Virtualenv if you just want a clean, isolated sandbox for a project and don't care about strict version locking.
Hopefully, this article helps someone avoid making the same mistakes I did. If you like it or hate it, leave a comment. Got anything to add? Be my guest. Keep your opinion to yourself. Later, skater.
Top comments (0)