AI Computer Institute
Expert-curated CS & AI curriculum aligned to CBSE standards. A bharath.ai initiative. About Us

Virtual Env

📚 Technology⏱️ 16 min read🎓 Grade 8
✍️ AI Computer Institute Editorial Team Updated: August 2026 CBSE-aligned · Peer-reviewed · 16 min read
Content curated by subject matter experts with IIT/NIT backgrounds. All chapters are fact-checked against official CBSE/NCERT syllabi.

Imagine you and your friend are both building science-fair projects in Python. You are making a chatbot that needs a library called requests version 2.25. Your friend is making a game that needs a totally different library. Now imagine that your school has just ONE shared computer for both projects, and every library anyone installs gets dumped into the same big box. You install your version of requests. Then a senior student installs a newer version 2.31 for their project, which quietly replaces yours. The next morning you run your chatbot and it crashes with a strange error. You changed nothing, yet your code broke. What happened?

This is one of the most common and most frustrating problems in real programming, and it has a clean solution called a virtual environment (usually shortened to venv). By the end of this chapter you will understand exactly what a virtual environment is, why every professional Python programmer uses them, how to create and use one with real commands, and what is actually happening inside your computer when you do. This is not abstract theory. This is the very first thing a working developer sets up before writing a single line of a new project.

The Real Problem: One Box for Everything

When you install Python on a computer, it comes with a single, shared storage area where all your installed libraries (also called packages) live. Think of it as one giant toolbox for the whole machine. When you run a command like pip install pandas, the pandas library gets placed into that one shared toolbox. Every Python program on the computer then reaches into that same toolbox.

That sounds convenient until you have more than one project. Consider two real projects an Indian student might build:

  • Project A — a UPI transaction analyzer that reads a bank statement CSV. It was written last year and depends on pandas version 1.5.
  • Project B — an ISRO satellite-data plotter you are building today. It uses a brand-new feature that only exists in pandas version 2.2.

Both projects want pandas, but different versions of it. With one shared toolbox, you can only keep one version at a time. Install version 2.2 for Project B, and Project A silently breaks because the old feature it relied on was removed. Install version 1.5 for Project A, and Project B cannot run. You are stuck. This is famously nicknamed "dependency hell."

A virtual environment solves this by giving each project its own private toolbox. Project A gets its own folder containing exactly pandas 1.5. Project B gets a completely separate folder containing pandas 2.2. Neither can touch the other. They can happily coexist on the same laptop, and neither can break the other. That is the entire core idea.

The Analogy That Makes It Click

Think of your computer like a large apartment building, and Python's shared toolbox as one common kitchen that everyone must share. If your neighbour swaps the sugar jar for salt, your recipe is ruined even though you never touched anything. A virtual environment is like giving every family its own separate kitchen inside their own flat. Your ingredients are yours. What your neighbour stocks in their kitchen has zero effect on yours. You can keep old spices; they can keep new ones. Nobody's cooking gets sabotaged.

Here is the diagram of what is really happening on your machine:

One System Python, Two Private Toolboxes System Python the interpreter engine venv: upi-analyzer pandas 1.5 requests 2.25 Project A's private packages venv: isro-plotter pandas 2.2 matplotlib 3.9 Project B's private packages The two toolboxes never touch each other.

Notice one important detail in the diagram: there is still only one System Python engine at the top. A virtual environment does not install a whole new copy of Python. It creates a lightweight private folder for packages and points back to the same underlying Python interpreter. This is why creating a venv is fast and takes very little disk space.

Creating Your First Virtual Environment

Python ships with a built-in tool called venv. You do not need to install anything extra. Let us walk through the exact commands, step by step. Suppose you are starting the ISRO plotter project.

Step 1 — Create the environment. Open a terminal, move into your project folder, and run:

python -m venv myenv

Let us read this command carefully because every part matters. python launches Python. The flag -m venv tells Python "run the built-in module named venv." The final word myenv is the name of the folder that will be created to hold this environment. You can name it anything; myenv, venv, and .venv are all common choices. After running this, a new folder called myenv appears, containing a private copy of pip, a link to Python, and an empty place for your packages.

Step 2 — Activate the environment. Creating the folder is not enough; you must "step inside" it so your terminal knows to use it. This is called activating. The command differs by operating system:

# On Windows (Command Prompt):
myenv\Scripts\activate

# On macOS or Linux:
source myenv/bin/activate

Once activated, your terminal prompt changes to show the environment name in parentheses, like this:

(myenv) C:\Users\Aarav\isro-plotter>

That (myenv) prefix is your signal that you are now working inside the private toolbox. Any package you install now goes into myenv, not the shared system toolbox.

Step 3 — Install packages. Now install exactly what this project needs:

pip install pandas matplotlib

These land inside myenv only. Your other projects, and the system Python, are completely untouched.

Step 4 — Deactivate when done. When you finish working, step back out with a single word:

deactivate

The (myenv) prefix disappears, and your terminal returns to the normal system Python. Your private toolbox stays safely on disk, ready for next time you activate it.

Proving It Works: A Traced Example

Let us actually verify that a virtual environment isolates packages, by tracing a short session line by line. Suppose your system Python has no pandas installed at all. Watch what happens:

python -m venv myenv
source myenv/bin/activate
pip install pandas
python -c "import pandas; print(pandas.__version__)"

Trace it: the first line builds the myenv folder. The second activates it, so (myenv) now prefixes the prompt. The third installs pandas into myenv. The fourth runs a tiny one-line program (the -c flag means "run this code string"). It imports pandas and prints its version. The output is a version number, for example:

2.2.2

Now deactivate and try the exact same import against the system Python:

deactivate
python -c "import pandas; print(pandas.__version__)"

This time, because the system Python never had pandas, Python cannot find it and raises an error:

ModuleNotFoundError: No module named 'pandas'

Read what this proves. The same import statement succeeded inside the environment and failed outside it. That is isolation working exactly as designed. The package truly lived only inside myenv and nowhere else. If a package could leak out of the environment, the second command would have printed a version number too. It did not.

Sharing Your Project: requirements.txt

Here is where virtual environments become genuinely powerful for teamwork and for competitive projects. Suppose you want to submit your ISRO plotter to a coding competition, or share it with a teammate in Bengaluru. They need the exact same packages and versions you used, or your code might behave differently on their machine.

You do not list packages by hand. With your environment activated, you run:

pip freeze > requirements.txt

The pip freeze command lists every installed package and its exact version. The > symbol redirects that list into a file named requirements.txt. Open the file and you will see something like:

matplotlib==3.9.2
numpy==2.1.1
pandas==2.2.2

The double equals sign == pins each package to an exact version. Now your teammate creates their own fresh virtual environment on their laptop and runs a single command:

pip install -r requirements.txt

The -r flag means "read this requirements file and install everything listed in it." In seconds, their environment becomes an exact mirror of yours: same packages, same versions. Your code now runs identically on both machines. This one file is how millions of real Python projects, including nearly everything on GitHub, guarantee that "it works on my machine" also means "it works on yours."

A Common Misconception, Cleared Up

Many students first believe that a virtual environment installs a separate, brand-new copy of Python for each project. This is wrong, and understanding why sharpens your whole mental model.

A virtual environment does not duplicate the Python interpreter. Downloading and installing full Python takes tens of megabytes and some time; creating a venv is nearly instant and tiny. What actually happens is that the venv creates a small folder containing its own pip, its own isolated site-packages area (the private toolbox where libraries go), and a lightweight link back to the one system Python interpreter already on your machine. When you run python inside an active venv, you are running the same underlying engine, but Python has been told: "look for and store packages in this project's folder, not the shared one." So the thing that is isolated is your packages, not the Python engine itself. This is exactly why you cannot use a venv to run Python 3.12 code on a machine that only has Python 3.9 installed; the venv borrows whatever Python already exists.

A second frequent confusion: students forget to activate the environment, install a package, then wonder why it went into the wrong place. Remember, creating a venv and using a venv are two separate steps. If you do not see the (myenv) prefix in your prompt, you are not inside the environment, and pip install will drop packages into the shared system toolbox. Always glance at your prompt first.

Why Professionals Never Skip This

You might think virtual environments are only for people juggling many projects. But there is a deeper reason every serious developer uses them from day one. Some Python tools that come pre-installed with your operating system depend on specific package versions. If you install and upgrade packages in the shared system toolbox, you can accidentally break parts of your operating system itself. A virtual environment keeps your experiments walled off, so a messy install can never damage anything outside that one folder. If a project's environment gets hopelessly tangled, you simply delete the myenv folder and build a fresh one in seconds. Nothing else on your computer is affected. This "safe to throw away and rebuild" property is enormously reassuring when you are learning and inevitably make mistakes.

This is also why the Open EdTech offline-AI-tutor style projects, and essentially every AI project you will build with libraries like numpy, torch, or transformers, start with creating a virtual environment. AI libraries are large, fast-changing, and extremely version-sensitive; two AI projects almost never want the exact same versions. Isolation is not a nicety here, it is a requirement.

Quick Reference: The Whole Workflow

  1. python -m venv myenv — create the private toolbox.
  2. source myenv/bin/activate (or myenv\Scripts\activate on Windows) — step inside.
  3. pip install ... — install packages, isolated to this project.
  4. pip freeze > requirements.txt — record exact versions for sharing.
  5. deactivate — step back out when done.
  6. pip install -r requirements.txt — rebuild the same environment anywhere.

Active Recall: Test Yourself

Do not just read these; actually work out each answer before checking your reasoning against the chapter.

  1. Predict the output. You run python -m venv myenv and immediately, without activating, run pip install requests. Where does requests get installed: inside myenv, or in the shared system toolbox? Explain why. (Hint: which prefix is showing in your prompt?)
  2. Spot the bug. Your friend says "I created a venv, so now Python 3.12 features will work even though my laptop only has Python 3.9." Is your friend right? Explain in one sentence what a venv actually isolates.
  3. Design task. You have two projects: a cricket-score scraper needing requests 2.25, and a news scraper needing requests 2.31. Write the sequence of commands to set up isolated environments for both so neither breaks the other.
  4. Explain the file. A classmate sends you their project with a requirements.txt but you get errors running their code. What two commands should you run, in order, to reproduce their exact setup on your machine?
  5. Reason about disk. Creating a venv is nearly instant and uses very little space, while installing Python itself is much slower and larger. Using what you learned about what a venv actually contains, explain why.

Summary

A virtual environment is a private, isolated folder that holds one project's Python packages, separate from every other project and from the system-wide packages. It solves dependency hell, where different projects need conflicting versions of the same library, by giving each project its own toolbox. You create one with python -m venv myenv, enter it by activating (watch for the (myenv) prompt prefix), install packages that stay isolated inside it, and leave with deactivate. The requirements.txt file, produced by pip freeze and consumed by pip install -r, lets anyone recreate your exact environment anywhere, which is how real projects stay reproducible across machines. Crucially, a venv isolates your packages, not the Python interpreter itself, which is why it is lightweight, safe to delete and rebuild, and the very first step every professional takes when starting a new Python project.

Practice Exercises

Now it is time to practice! Complete these challenges to solidify your understanding:

  • Exercise 1: Write a short program that demonstrates the core concept from this chapter. Test it with at least 3 different inputs.
  • Exercise 2: Find a real-world example where virtual env is used in an Indian company (like TCS, Infosys, Flipkart, or ISRO). Write a paragraph explaining the connection.
  • Exercise 3: Create a mind-map connecting virtual env to at least 3 other topics you have studied.
← Env VarsPip and Package Management: Standing on Giants →

Found this useful? Share it!

📱 WhatsApp 🐦 Twitter 💼 LinkedIn