Python Virtual Environment Setup Guides

Posted on Feb 15, 2025

Python Virtual Environment Setup Guides

This page explains how to create and manage Python virtual environments across Linux, Unix, macOS, and Windows. Virtual environments isolate dependencies for each project, preventing version conflicts.


📌 Navigation


Linux & Unix

Create a Virtual Environment

python3 -m venv venv

Activate

source venv/bin/activate

Install Packages

pip install requests

Deactivate

deactivate

💡 Tip: Store your environment in a .venv folder inside your project directory for clarity.


macOS

Install Python 3 (if not already installed)

brew install python

or download from python.org

Create Environment

python3 -m venv venv

Activate

Alternative: Multiple Python Versions

Use pyenv and pyenv-virtualenv:

brew install pyenv pyenv-virtualenv

This allows you to manage multiple versions of Python without affecting the system installation.


Windows

Create Virtual Environment

py -m venv .venv

Activate

..venv\Scripts\activate

Install Packages

pip install requests

Deactivate

deactivate

💡 Note: Use the Python Launcher (py) for easier version management.


Additional Notes

  • The venv module is included in Python 3.3+.
  • Always add .venv/ to .gitignore:
.venv/
  • Virtual environments prevent dependency conflicts between projects.

Summary Table

OSCreate CommandActivate CommandDeactivate Command
Linux/Unixpython3 -m venv .venvsource .venv/bin/activatedeactivate
macOSpython3 -m venv .venvsource .venv/bin/activatedeactivate
Windowspy -m venv .venv.\\.venv\\Scripts\\activatedeactivate