Fundamentals 7 min read

Creating and Managing Python Virtual Environments with venv and Pipenv

This guide explains what Python virtual environments are, how to create and activate them using the built‑in venv module or Pipenv, and provides detailed commands for installing, updating, and removing packages as well as best‑practice recommendations for maintaining clean, reproducible development setups.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Creating and Managing Python Virtual Environments with venv and Pipenv

In Python development, the term “env” usually refers to a virtual environment, which is a self‑contained directory containing an isolated Python interpreter copy and all installed libraries, allowing projects to have independent dependencies without affecting the global system.

Creating and managing virtual environments:

venv: The standard library module available in Python 3.3+ for creating lightweight virtual environments.

Steps to create a venv:

python3 -m venv myenv  # myenv is the name of the virtual environment

Activate the virtual environment (commands differ by OS):

On Unix/Linux/macOS: source myenv/bin/activate On Windows: myenv\Scripts\activate.bat pipenv: A higher‑level tool that manages both virtual environments and project dependencies via Pipfile and Pipfile.lock, providing better dependency locking and team consistency.

Typical pipenv workflow:

pip install pipenv  # install pipenv if not already installed</code>
<code>pipenv --python 3.8 myenv  # create a virtual environment with a specific Python version</code>
<code>pipenv shell  # automatically activate the environment

Package management commands:

In a venv:

Install a package (e.g., requests): pip install requests Update a package: pip install --upgrade requests Uninstall a package: pip uninstall requests In a pipenv environment:

Install a package: pipenv install requests Update a specific package: pipenv update requests Update all packages (use with caution): pipenv update --all Uninstall a package: pipenv uninstall requests View installed packages:

venv: pip list</code>
<code>pipenv: pipenv lock -r</code>
<code>or, within an activated pipenv shell: pip list

Virtual environment best practices:

Create an independent virtual environment for each project: Ensures dependencies are isolated and prevents conflicts.

Regularly update environments: Keeps packages current to avoid security and compatibility issues, but verify updates do not break existing code.

Ignore virtual environment files in version control: Use .gitignore to exclude directories like .venv or generated Pipenv files; share requirements.txt or Pipfile instead.

Manage configuration with environment variables: Store sensitive data such as DB connection strings or API keys outside code, using libraries like python‑decouple.

Advanced tips:

Cross‑platform compatibility: Use tools like pyproject.toml (PEP 621) for unified project metadata.

Environment variable injection: Load .env files with dotenv for development and testing.

Performance optimization: Use pip’s --no-cache-dir, --no-deps, or --only-binary=:all: options to reduce install time and disk usage.

Debugging and logging: Add -v/--verbose to pip commands for detailed output; consider pip‑tools for fine‑grained requirements management.

Containerization: For reproducible deployments, containerize the application with Docker, specifying a Python base image and installing dependencies in the Dockerfile.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythondependency managementvirtual environmentpipenvvenv
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.