Difference between pipenv and python -m venv Environments in Python
Both pipenv and python -m venv are tools used to create isolated Python environments, but they serve slightly different purposes and have different features.
Here's a side-by-side comparison:
๐ Pipenv vs python -m venv
| Feature | pipenv |
python -m venv |
|---|---|---|
| ๐ง Purpose | Virtualenv + Dependency Management Tool | Just creates a virtual environment |
๐ฆ Handles requirements.txt
|
Uses Pipfile and Pipfile.lock instead |
You manage requirements.txt manually |
| ๐ Dependency resolution | Automatically resolves compatible versions | You install packages manually |
| ๐งช Automatically installs pip | โ Yes | โ Yes |
| ๐ Easy to see dependencies | pipenv graph |
No built-in dependency graph viewer |
๐ Creates .venv folder |
โ Yes | โ
Yes (default is .venv or env) |
| โก Run scripts inside env | pipenv run your_script.py |
.\venv\Scripts\python your_script.py |
| ๐ฌ Enter shell | pipenv shell |
.\venv\Scripts\activate (Windows) |
| ๐ Lockfile support | โ
(Pipfile.lock) |
โ You must create requirements.txt
|
| ๐ Version pinning | โ Built-in | Manual (you pin versions in requirements.txt) |
๐ก Summary
-
Use
python -m venvif:- You want a lightweight, simple virtual environment
- You prefer managing dependencies manually with
pip install+requirements.txt
-
Use
pipenvif:- You want an all-in-one tool for managing dependencies and virtual environments
- You like automatic lockfile creation and version resolution
- You want a
pip install+venvcombo with nicer CLI commands
๐ง Real-World Example
Using python -m venv:
python -m venv venv
.\venv\Scripts\activate
pip install fastapi
pip freeze > requirements.txt
Using pipenv:
pipenv install fastapi
pipenv shell
# Pipfile and Pipfile.lock are auto-managed
Top comments (0)