How to Install Python and Use venv on Windows, macOS, and Linux

Create isolated environments with python -m venv, activate them on each OS, and manage dependencies with pip.

Languages & runtimes Beginner 6 min read

·

Last verified: March 2026. Python’s venv module exists so each project can pin its own libraries without breaking system tools that also use Python. You run python -m venv because it creates an isolated interpreter layout; you run pip only after activation so packages install into that sandbox, not globally.

Install Python 3

Why install before venv: The venv module ships with Python 3, but Linux distros sometimes split python3-venv into a package—you need a working python3 binary first.

Windows

Download the current Python 3 installer from python.org. Enable Add python.exe to PATH so terminals find Python without manual PATH edits—without PATH, later commands fail with “not recognized”.

Choose Install Now. Open a new terminal (PATH is read at launch) and verify the install:

python --version

Check: You see a 3.x version. If not, reopen the terminal or repair PATH.

macOS

Use the official installer, or brew install python if you use Homebrew—Homebrew keeps upgrades predictable for CLI workflows.

python3 --version

Linux

Package managers ensure OpenSSL and sqlite bindings match your distro—important so pip wheels install cleanly.

sudo apt install python3 python3-venv python3-pip
python3 --version

Create a virtual environment

Why python3 -m venv .venv: The -m venv form guarantees you use the same interpreter that will run your project; calling a stray venv script might point at the wrong Python.

From your project directory:

python3 -m venv .venv

On Windows, if python3 is missing, try python -m venv .venv. This creates a .venv folder with its own interpreter and site-packages.

Check: ls .venv (or dir .venv) shows bin or Scripts and a pyvenv.cfg file describing the base interpreter.

Activate the venv

Why activation: Shells search PATH for commands; activation prepends the venv’s bin (or Scripts) so python and pip resolve to the project, not the system.

  • Windows (cmd): .venv\Scripts\activate.bat
  • Windows (PowerShell): .venv\Scripts\Activate.ps1 (you may need execution policy adjustments)
  • macOS / Linux: source .venv/bin/activate

Your prompt usually shows (.venv). Confirm you are on the right interpreter:

which python
python -c "import sys; print(sys.prefix)"

Check: The path should live inside your project’s .venv directory.

Install packages with pip

Why pip install after activation: Otherwise packages land in the user or system site-packages and collide across projects.

pip install requests
pip freeze > requirements.txt

Why pip freeze: It records exact installed versions so teammates and CI can reproduce the same dependency tree with pip install -r requirements.txt.

Commit requirements.txt (not .venv/). For reproducible builds, consider pinning versions or using a lock tool for larger projects.

PATH and “command not found”

Why this happens: Installers update PATH for new processes; old terminals keep the old environment. On Linux, python vs python3 split exists because Python 2 historically owned the python name.

Reopen the terminal after installing on Windows. On Linux/macOS, prefer python3 explicitly if python still points to Python 2 on older systems.

Related guides

Keep API keys out of your repo: see Environment variables and secrets. For Node-based tools alongside Python, see Install Node.js and npm.

Frequently asked questions

Should I use venv or virtualenv?

For Python 3.5+, built-in venv is enough for most developers. Third-party virtualenv adds extra features some teams still prefer.

Do I commit the .venv folder?

No. Add .venv/ to .gitignore. Commit requirements.txt (or your chosen lock file) instead.

How do I deactivate?

Run deactivate (works on all platforms once the venv is active).

Can I move a venv?

Avoid moving or renaming the folder; paths are baked in. Delete .venv and recreate with python -m venv .venv, then reinstall packages.