Getting Started with venv

# 1. Create a venv (usually named .venv by convention)
python3 -m venv myenv

# 2. Activate - Windows (PowerShell)
myenv\Scripts\Activate.ps1

# 3. Install packages - installs only into this venv
pip install requests numpy

# 4. Use python as typical

# 5. Deactivate when done
deactivate

What if you close without deactivate

If you close the Windows Terminal without deactivating, it’s ok. Nothing bad happens. The venv is just a folder on disk. Deactivating only removes some of the temp environment variables from your current shell session. A new console those variables are gone, effectively the same as deactivating.

Sharing Requirements

You can also install dependencies from a list, or save your current ones. This is useful when sharing a project, instead of committing the venv folder (which is large and platform-specific), you commit requirements.txt so others can recreate the same environment with one command:

pip install -r requirements.txt

# Optional, but needed if sharing the project
pip freeze > requirements.txt

Tips

  • name it .venv, most editors auto-detect
  • add to .gitignore, never commit venv folder itself, only requirements.txt
  • one venv per project
  • if you have multiple python versions, specify which one to use: python3.11 -m venv .venv

What does the -m argument do?

The -m flag tells python to run a module from the standard library (or installed packages) as a script. So python -m venv means “run the venv module”, rather than calling a script file directly.

Why use it instead of just a command?

  • pip install ... runs the pip command, which might be a different pip than your current Python
  • python -m pip install ... - guarantees you’re using the pip that belongs to this specific Python
python -m venv .venv # uses the venv module from this python