Python Modules & Packages
Python Virtual Environments
A virtual environment is an isolated, self-contained Python setup for a single project, so its packages never clash with any other project on your computer.
Why Isolate Your Projects?
Imagine Project A needs Django version 3, but Project B needs Django version 5. If you install packages globally, only one version can exist at a time and something breaks. A virtual environment gives each project its own private folder of packages, completely separate from the rest of your system.
Creating a Virtual Environment
Python includes the venv module out of the box, no extra installation required. Run it inside your project folder to create a new isolated environment.
python -m venv envThis creates a folder named env containing a fresh, isolated copy of Python and pip.
Activating the Environment
Activation switches your terminal to use the environment's own Python and pip instead of the system-wide ones. The command differs slightly by operating system.
source env/bin/activateenv\Scripts\activateOnce activated, your terminal prompt usually shows the environment name in parentheses, like (env), confirming it is active.
Installing Packages Inside the Environment
While activated, any pip install only affects this project's environment, leaving your global Python untouched.
pip install flask
pip freeze > requirements.txtDeactivating
When you are done working, simply run deactivate to return to your normal system Python.
deactivateAdd your env folder to a .gitignore file — never commit it to version control. Share requirements.txt instead so others can rebuild the same environment.
