1. The Problem: "Dependency Hell" Illustrated
Imagine you have two Python projects:
-
Project A (Legacy App): Uses an older version of a library, say `requests==2.20.0`.
-
Project B (New Feature): Requires a newer version of the same library, `requests==2.28.1`, due to new features or security updates.
If you `pip install requests==2.20.0` globally, then later `pip install requests==2.28.1` globally, the latter command will likely overwrite the former, breaking Project A. Virtual environments solve this by giving each project its own `requests` installation.
3. Using the `venv` Module (Standard Library)
The `venv` module is Python's built-in way to create virtual environments, available since Python 3.3. It's simple, effective, and doesn't require any external tools.
Step 1: Create a Project Directory
It's good practice to create your virtual environment inside your project's root directory.
$ mkdir my_udaan_project
$ cd my_udaan_project
Step 2: Create the Virtual Environment
Use `python -m venv `. A common convention is to name it `venv` or `.venv`.
$ python3 -m venv venv_udaan
# On some systems, it might just be 'python -m venv venv_udaan'
# This creates a directory named 'venv_udaan' in your current project.
Check Python Version: Ensure you use `python3` (or `python` if that points to Python 3.x) to create the `venv` with the desired Python version. For example, `python3.9 -m venv my_env` if you have multiple Python 3 versions installed.
Step 3: Activate the Virtual Environment
Activation modifies your shell's `PATH` variable so that `python` and `pip` commands point to the ones inside your virtual environment.
# For Linux / macOS:
$ source venv_udaan/bin/activate
(venv_udaan) $ # Your prompt will change to indicate the active venv!
# For Windows (Command Prompt):
C:\my_udaan_project>venv_udaan\Scripts\activate.bat
(venv_udaan) C:\my_udaan_project>
# For Windows (PowerShell):
PS C:\my_udaan_project>.\venv_udaan\Scripts\Activate.ps1
(venv_udaan) PS C:\my_udaan_project>
Troubleshooting Windows PowerShell: If you get an error about script execution, you might need to change your PowerShell execution policy. Run `Set-ExecutionPolicy RemoteSigned -Scope CurrentUser` in an administrative PowerShell, then try activating again.
Step 4: Install Dependencies with `pip`
Now that your virtual environment is active, any packages you install with `pip` will go into this isolated environment.
(venv_udaan) $ pip install requests numpy
Collecting requests
... (installation output) ...
Successfully installed certifi-xxxxxxx charset-normalizer-xxxxxxx idna-xxxxxxx requests-xxxxxxx urllib3-xxxxxxx numpy-xxxxxxx
(venv_udaan) $ pip list
Package Version
------------------ -------
numpy 1.26.4
pip 24.0
requests 2.31.0
setuptools 69.5.1
...
Step 5: Deactivate the Virtual Environment
When you're done working on your project, you can deactivate the environment. This reverts your `python` and `pip` commands back to the system's global ones.
(venv_udaan) $ deactivate
$ # Your prompt returns to normal
$ pip list | grep requests # Check if requests is still globally installed (it shouldn't be by default)
# (No output or older version if it was previously installed globally)
Step 6: Generate `requirements.txt` for Sharing
This is a critical step for project collaboration and deployment. The `pip freeze` command outputs all installed packages and their exact versions to standard output, which you then redirect to a file named `requirements.txt`.
$ source venv_udaan/bin/activate # Activate if not already active
(venv_udaan) $ pip freeze > requirements.txt
(venv_udaan) $ cat requirements.txt # View the contents of the file
certifi==2024.2.2
charset-normalizer==3.3.2
idna==3.7
numpy==1.26.4
requests==2.31.0
urllib3==2.2.1
(venv_udaan) $ deactivate
Step 7: Install from `requirements.txt` (for new users/deployment)
If you (or another developer) clone a project, you can easily set up the exact environment.
$ # Imagine you are in a *new* project directory (e.g., my_udaan_project_copy)
$ # where requirements.txt is present, but no venv yet.
$ python3 -m venv venv_udaan_copy
$ source venv_udaan_copy/bin/activate
(venv_udaan_copy) $ pip install -r requirements.txt
Collecting certifi==2024.2.2
... (installation output) ...
Successfully installed certifi-2024.2.2 ... numpy-1.26.4 requests-2.31.0 ...