Python Packaging & Environments
Table of Contents
- What is
pip? - What is
venv? - What is PyPI?
- How They Work Together
- Creating and Using Virtual Environments
- Activation (Windows vs Linux/macOS)
- Installing Packages into a
venv - Deactivating and Deleting Environments
- Requirements Files
- Best Practices
- Cheat Sheet
1. What is pip?
pip is Python’s package manager.
It allows you to:
- Install libraries
- Upgrade libraries
- Remove libraries
- Manage dependencies
Example:
pip install requests
2. What is venv?
venv (Virtual Environment) is a tool that creates isolated Python environments.
Why?
- Prevent dependency conflicts
- Use different versions of packages per project
- Keep your global Python clean
Each venv contains:
- Its own Python interpreter
- Its own pip
- Its own installed packages
3. What is PyPI?
PyPI (Python Package Index) is the central repository of Python packages.
Website: https://pypi.org
Contains thousands of libraries
pip downloads packages from PyPI by default
4. How They Work Together
Flow:
PyPI (online registry)
↓
pip install
↓
Virtual Environment (venv)
↓
Your Python Project
Explanation:
You run pip install flask pip connects to PyPI Downloads the package Installs it inside your active venv
Important:
- a. If no venv is active → installs globally
- b. If venv is active → installs locally inside it
5. Creating a Virtual Environment
Create a venv
python -m venv myenv
This creates a folder:
myenv/ ├── bin/ (Linux/macOS) ├── Scripts/ (Windows) ├── lib/ ├── pyvenv.cfg
6. Activating the Virtual Environment
On Windows (CMD)
myenv\Scripts\activate
On Windows (PowerShell)
myenv\Scripts\Activate.ps1
On Linux/macOS
source myenv/bin/activate
7. What Happens When You Activate?
- Your shell PATH is modified
- python now points to the venv interpreter
- pip installs inside the venv
You will see:
(myenv) user@machine:~
8. Installing Packages into the venv
pip install flask
pip install requests
Check installed packages:
pip list
Show details:
pip show flask
9. Deactivating the Environment
deactivate
This returns you to the global Python environment.
10. Deleting a Virtual Environment
Just delete the folder:
rm -rf myenv # Linux/macOS
rmdir /s myenv # Windows
11. Requirements Files
Save dependencies:
pip freeze > requirements.txt
Install from file:
pip install -r requirements.txt
Example requirements.txt:
flask==2.3.2
requests==2.31.0
12. Best Practices
- Always use venv per project
- Never install packages globally unless necessary
- Use requirements.txt
- Name environments clearly (venv, .venv, env)
- Add venv to .gitignore
Example .gitignore:
venv/
.env/
13. Cheat Sheet
```bash
Create venv
python -m venv myenv #Activate #Windows CMD: myenv\Scripts\activate
#Windows PowerShell:
myenv\Scripts\Activate.ps1
#Linux/macOS:
source myenv/bin/activate
#Install package pip install package_name
#Upgrade package pip install –upgrade package_name
#Uninstall package pip uninstall package_name
#List packages pip list
#Freeze dependencies pip freeze > requirements.txt
#Install from requirements pip install -r requirements.txt
#Deactivate venv deactivate