Seekvana
Building with AIbeginner

Python venv Explained: Create, Activate, and Use It

Python venv explained simply: what it is, why every project needs one, and the exact commands to create, activate, and use it, including the #1 beginner mistake.

SeekvanaJuly 4, 20266 min read
Two separate folders side by side, each holding its own Python logo and package icons, clay accent outlines

A virtual environment is a private, isolated copy of Python for one project, its own interpreter, its own installed packages, completely separate from every other project on your machine. This lesson explains why that isolation matters and gives you the exact commands to create, activate, and use one.

By the end, you'll have a venv running in your project folder with the anthropic package installed inside it, not floating around globally.

Key Takeaways

  • A venv is a folder holding its own Python interpreter and its own packages, isolated from your system Python
  • Without one, two projects needing different versions of the same package will conflict, one always breaks
  • python -m venv venv creates it; activating it changes your terminal prompt to show (venv)
  • The #1 beginner mistake: running pip install before activating, which installs globally instead of into the project
  • venv/ always goes in .gitignore, it's rebuilt from requirements.txt, never committed

What a Virtual Environment Actually Is

Every Python installation on your computer is, by default, one shared, global thing. Every project you write pulls its packages from that same single pool.

A virtual environment breaks that pool apart. Running python -m venv venv creates a folder containing a private copy of the Python interpreter and an empty space for packages, tied to that one project only. Installing a package while that venv is active puts it inside that folder, not into your system-wide Python.

Think of it as giving each project its own toolbox, instead of every project reaching into one shared drawer where tools get moved, replaced, or go missing.

Why You Need One

Here's the conflict a venv prevents. Say Project A needs Django 4.0, and Project B, sitting in a folder right next to it, needs Django 3.2.

With one global Python, only one version of Django can exist at a time. Install 4.0 for Project A, and Project B breaks. Install 3.2 for Project B, and Project A breaks. There's no way to satisfy both from the same shared pool.

Give each project its own venv, and the conflict disappears entirely. Project A's venv holds Django 4.0. Project B's venv holds Django 3.2. Neither knows the other exists.

This isn't a rare edge case, it's the default state of having more than one Python project on your machine. The moment you have two, you need this.

Diagram comparing a shared global Python environment causing a Django version conflict against isolated per-project venvs, plus a terminal showing the prompt before and after activation
No venv: Project A and B fight over one Django version. With venv: each project keeps its own, and the (venv) prefix confirms which one is active.

Creating and Activating a venv

Create the venv

From your project folder, run:

python -m venv venv

This creates a folder named venv containing a private Python interpreter. The first venv is the module doing the work, the second is just the folder name, you could call it anything, but venv is the convention everyone follows.

Activate it

On Mac/Linux:

source venv/bin/activate

On Windows:

venv\Scripts\activate

Your terminal prompt now shows (venv) at the start of the line. That prefix is your confirmation, everything you install from this point runs inside the isolated environment, not your global Python.

Deactivate when you're done

deactivate

One word, no arguments. The (venv) prefix disappears, and your terminal returns to using system Python. Your project files are untouched either way, deactivating just changes which Python interpreter is currently active.

The #1 Beginner Mistake

Forgetting to activate before running pip install is the single most common venv mistake, and it's silent. The command runs, packages appear to install fine, no error at all.

The problem only surfaces later: your script can't find a package that you're certain you installed, because it went into your global Python, not the project's venv.

If pip install anthropic succeeds but your script still says ModuleNotFoundError: No module named 'anthropic', check your prompt for (venv) first. Nine times out of ten, the package installed globally because the venv wasn't active when you ran the command.

The fix is always the same: activate first, then install. Order matters here exactly like it did with load_dotenv() in lesson 05.08, the environment has to be set up before the command that depends on it runs.

Deleting a broken venv is always safe. It's just a folder of copied files, none of it is your actual project code. Delete it, recreate it with python -m venv venv, reactivate, and reinstall.


Your Task

Create a venv and install anthropic inside it

In your project folder, run:

python -m venv venv

Activate it (Mac/Linux: source venv/bin/activate, Windows: venv\Scripts\activate). Confirm (venv) appears in your prompt. Then install the anthropic package, the one you'll use for every Claude API call in this course, from inside the active venv:

pip install anthropic

Run pip list and confirm anthropic appears in the output.

Done? You've completed Lesson 05.12, and Module 05. Next up: Module 06, Web Basics, where you'll learn how HTML, CSS, and JavaScript work together →

FAQ

Common questions

  • One venv per project, always. Sharing defeats the entire purpose, the moment two projects need different versions of the same package, a shared venv puts you right back in the conflict it was meant to prevent. Creating a new one takes seconds and costs nothing but a few megabytes of disk space.

  • Yes, safely. A venv folder holds nothing but a copied interpreter and installed packages, none of it is your actual code. Delete the folder, run python -m venv venv again, reactivate, and reinstall your packages from requirements.txt if you have one. Nothing about your project files is affected.

  • Because it's a real folder full of files, and git tracks everything by default. Add venv/ to your .gitignore immediately, virtual environments are never committed, they're rebuilt fresh on every machine from your requirements.txt instead. Committing one bloats your repo with files that won't even work on someone else's computer.

  • They solve different problems. pip installs packages; venv decides where those packages go. Without an active venv, pip install targets your one global Python installation. With a venv active, the same pip install command targets that project's isolated folder instead, same tool, different destination.

Finished reading?

Mark it complete to track your progress through the path.


Was this article helpful?

Comments (0)

0/1000

Be the first to leave a comment.