DEV Community

Muhammad Atif Iqbal
Muhammad Atif Iqbal

Posted on

Difference between `pipenv` and `python -m venv` Environments

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 venv if:

    • You want a lightweight, simple virtual environment
    • You prefer managing dependencies manually with pip install + requirements.txt
  • Use pipenv if:

    • 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 + venv combo 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
Enter fullscreen mode Exit fullscreen mode

Using pipenv:

pipenv install fastapi
pipenv shell
# Pipfile and Pipfile.lock are auto-managed
Enter fullscreen mode Exit fullscreen mode

Top comments (0)