Python Virtual Environments
Why do you need a virtual environment for python?
A virtual environment is a self-contained directory that contains a Python installation for a particular version of Python, plus a number of additional packages. It allows you to manage dependencies for different projects separately, avoiding conflicts between them. This is especially important when working on multiple projects that may require different versions of the same package.
Why You Need It
- Stops Version Fights: Different projects might need different versions of the same tool. One project might need an old version, while a new project needs a recent update.
- Keeps Your Computer Clean: Installing every tool globally clutters your main Python installation and risks breaking your operating system's internal scripts.
- Makes Sharing Easy: You can export an exact list of your project tools into a text file. Other people can then recreate your exact setup on their computers.
- Safe Testing: You can test new or experimental packages without changing your main setup, and delete the environment safely if you do not like them.
How It Works
- A Separate Folder: A virtual environment creates a small folder inside your project directory that holds its own copy of Python and its own library folder.
- Activation: Without uv you need to run a simple command in your terminal to turn the environment on before you start working.
- Isolation: Any package you install while the environment is active stays inside that specific folder and does not affect the rest of your computer.
If you install a dependency with uv do you still need to check that you are in the project 's environment?
One of the nice things about uv is that it automatically finds and uses the project's virtual environment, so you don't need to manually activate it first like you often would with venv + pip.
uv add[package-name]
- ✅ you don't need to check if you are in the project 's environment.
- ✅ uv automatically finds and uses the project's virtual environment.
- ✅ uv automatically installs the package into the virtual environment.
- ✅ uv automatically updates the pyproject.toml file to include the new dependency.
If you don't use uv, how do you manage virtual environments?
what does pip3 install do in terms of virtual environment?
Pip3 install does not directly install the package inside a virtual environment. Instead, it installs the package into the global site-packages directory on your Mac. This means that if you have multiple versions of Python installed on your Mac, pip3 will install the package in the version that is currently active.
When you run pip3 install, its behavior depends entirely on whether your virtual environment is active or inactive in your terminal.
If the Environment is Active (Turned On)
If the environment is active, pip3 will install the package into the virtual environment's private folder. This means that the package will only be available in the project you are currently working on.
pip3 install [package-name] Installs inside the virtual environment: The package goes into your virtual environment's private folder.
- Keeps system clean: It does not touch your Mac's global Python installation.
- Uses env's pip: The terminal automatically routes the pip3 command to use the package manager bundled inside your virtual environment.
If the Environment is Inactive (Turned Off)
If the environment is inactive, pip3 install [package-name] will install the package into your Mac's global Python installation. This means that the package will be available to all projects on your computer, but it may also cause conflicts with other packages or versions.
How to Check Before Installing
Before you run pip3 install, look at your terminal prompt. If a virtual environment is active, you will see its name in parentheses at the very beginning of the line:
- 🟢 Active (Safe): (myenv) user@MacBook ~ %
- 🔴 Inactive (Global): user@MacBook ~ %