Skip to content
IT Vizag
← Files, Modules & Packages

▶ Sign up free for the video, slides & prompts

The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.

Checking…

Virtual environments

Lesson 7 of 10 · 2 min read

Keep each project's libraries separate, with venv.

🎬 Video version coming soon — read it now

Project A needs pandas 1.5. Project B needs pandas 2.2. If both install into the same Python, one of them breaks. A virtual environment (venv) gives each project its own private set of packages.

Creating one

In your project folder, in a terminal:

🔒 Code example: sign up free (above) to unlock it.

This creates a .venv folder containing a copy of Python and an empty package collection.

Activating it

🔒 Code example: sign up free (above) to unlock it.

Your terminal prompt now starts with (.venv). Anything you pip install goes into this project only.

🔒 Code example: sign up free (above) to unlock it.

Type deactivate to leave it.

The everyday routine

  1. Make a project folder
  2. python -m venv .venv
  3. Activate it
  4. Install what you need
  5. pip freeze > requirements.txt
  6. Add .venv to .gitignore. Never upload the venv to GitHub, only requirements.txt

When someone else (or you, on a new laptop) gets the project:

🔒 Code example: sign up free (above) to unlock it.

Do I need this in Colab?

No. Each Colab notebook already runs in its own fresh machine. Virtual environments matter when you run Python on your own laptop, which is the next lesson.

Other tools you'll hear about

  • uv: a very fast modern replacement for pip and venv, increasingly popular
  • conda: common in data science, also manages non-Python libraries
  • poetry: manages dependencies and packaging together

They all solve the same problem. Learn venv first; the ideas carry over.

Tip: Your editor (VS Code) can detect the .venv folder and use it automatically. If an import works in the terminal but not in the editor, check which Python interpreter the editor has selected.

In one line

A virtual environment gives each project its own packages: create it with python -m venv .venv, activate it, and share requirements.txt.

Saved on this device. Sign in to keep progress everywhere.

Share with a friend