Contenido del curso

Python Virtual Environments Explained

Resumen

Working on multiple Python projects sounds harmless until you realize you need two different versions of the same library. Python virtual environments solve that exact headache by isolating dependencies per project, and learning to use them will make you a stronger developer from day one.

Why do you need virtual environments in Python?

When you install packages globally, every new install can overwrite the previous one. So if you install one version of pandas for project A and a different version for project B, project A breaks. That conflict is the main reason Python created Virtual Environments and Packages, a system designed to let you keep multiple versions of the same package on the same computer without collisions.

A virtual environment is essentially an extra folder on your machine that contains a real, working version of Python. When you create it, you decide which Python version lives inside, and any library you install only affects that folder.

What is a Python virtual environment? It is an isolated folder with its own Python executable and its own packages, so each project keeps the exact dependencies it needs without interfering with the rest.

How do virtual environments actually work?

Every time you create one, you get a Python executable tied to that folder. You can have many of them at once, and since they are just folders, you can delete one and create another whenever you want. You might end up with environment 1, environment 2, and environment 5, with no problem at all.

Inside each environment you keep a list of dependencies. To install the right versions for a project, you first activate the environment, then install. The activation step is what tells your terminal to use that specific Python instead of the global one.

When should you activate or deactivate an environment?

It depends on what you are doing. If you are working on a project that needs a specific Python or library version, activate the environment created for that project before running anything. When you switch projects, deactivate it or activate a different one.

Python gives you a script to activate the environment automatically. Once active, every time you run python, it executes the Python inside that environment.

Why do I get "module not found" in Python? Most of the time it means you forgot to activate the right virtual environment, so Python is looking for the package in the wrong place. Check which environment is active before running your code.

How do you install and manage packages with pip?

A fresh virtual environment starts empty, with zero packages installed. To add libraries you use pip, the dependency manager that comes with Python. With pip you can install, uninstall, and inspect packages inside the active environment.

A few useful actions you will repeat often:

  • Install a library with pip install followed by the package name.
  • Inspect details of an installed library with pip show requests, which returns the version and metadata.
  • List every dependency active in your current environment to confirm what is available.

Keep in mind that all of this only affects the environment you have activated, which is exactly the point.

How do you share dependencies with other developers?

If you hand your code to another developer and they do not have the same libraries installed, your code will fail on their machine. That is why most Python developers today use a requirements.txt file, a plain text file listing every dependency the project needs along with its specific version.

Pinning the version matters because your code might only be compatible with, say, version 1.9.2 of a library. By sharing requirements.txt, you are telling your team the exact setup where the project works.

What is requirements.txt in Python? It is a text file that lists all the libraries and versions a project depends on, so any developer can recreate the same environment with one command.

Does creating a virtual environment change between operating systems?

Yes, and this trips up a lot of beginners. The commands to create and activate environments are not identical across Windows, Mac, and Linux. Mac and Linux behave similarly because both are Unix based, so the syntax is almost the same. Windows is different, even the file paths use backslashes instead of forward slashes.

Because of this, the practical setup is split into dedicated lessons per operating system, so you can follow the exact steps for your machine without guessing.

Before moving on, open the official Python documentation on virtual environments and read it slowly. Get comfortable with the vocabulary: environment, activation, pip, dependency, requirements file. In the next lesson you will create your first environment locally and start installing packages the right way.

What is the first project where you plan to use a virtual environment? Share it in the comments.