📦 Learn how to manage third-party packages in Python using pip, understand the Python Package Index (PyPI), and isolate project dependencies using virtual environments.
This section covers:
- 🔍 What is PyPI and how to find and install packages
- 📦 How to use
pipfor installing, upgrading, listing, and uninstalling packages - 🧱 Managing package versions with semantic versioning
- 🧩 Working with virtual environments using
venvandpipenv - 💡 Best practices and hidden tips for managing dependencies effectively
- 📦 How to install and manage third-party packages via
pip - 🔍 How to search and install packages from the Python Package Index (PyPI)
- 🧱 Understanding semantic versioning (
major.minor.patch) - 🧩 Use
pip list,pip freeze, andpip showto inspect installed packages - 🧪 Create and manage isolated virtual environments using
venvandpipenv - 💡 Hidden notes on avoiding dependency conflicts, managing requirements, and setting up reproducible environments
The Python Package Index (PyPI) is the official repository of software for the Python programming language.
🔹 It hosts thousands of open-source libraries and tools contributed by the community.
🔹 You can search for packages using keywords like requests, numpy, or pandas.
- Go to https://pypi.org
- Use the search bar to find relevant packages.
- Click on the package name to view its documentation, version history, and installation instructions.
🔸 Popular packages include:
requests– HTTP requests made simplenumpy– Numerical computingpandas– Data manipulationflask– Lightweight web framework
pip is the default package manager for Python and comes pre-installed with Python 3.4+.
pip --versionIf not available, you can usually install it via:
python -m ensurepip --upgradepip install requestsInstall a specific version:
pip install requests==2.20.1pip install --upgrade requestspip listTo check outdated packages:
pip list --outdatedpip show requestsIncludes info like:
- Version
- Summary
- Author
- License
- Required dependencies
pip uninstall requestsYou’ll be prompted to confirm before removal.
Use pip freeze to export all installed packages and their exact versions:
pip freeze > requirements.txtThis file helps recreate the same environment elsewhere.
🔸 Example content of requirements.txt:
certifi==2025.1.31
charset-normalizer==3.4.1
idna==3.10
requests==2.32.3
urllib3==2.3.0
🔸 To install all dependencies from a file:
pip install -r requirements.txtPackages follow the format: major.minor.patch
| Part | Meaning |
|---|---|
| Major | Breaking changes – not backward compatible |
| Minor | New features added – still backward compatible |
| Patch | Bug fixes only – no new features |
🔹 Example:
requests==2.32.3– patch updaterequests==2.33.0– minor updaterequests==3.0.0– major update (may break existing code)
When working on multiple projects, different applications may require different versions of the same package — which can cause conflicts.
Virtual environments solve this by creating isolated Python environments for each project.
Python has built-in support for virtual environments via the venv module (available since Python 3.3).
mkdir myproject
cd myproject
python -m venv .venv.venv\Scripts\activatesource .venv/bin/activateYou’ll see something like:
(.venv) C:\myproject>
deactivateOnce activated, any package installed will be local to that environment.
pip install requestspip freeze > requirements.txtpip install -r requirements.txtpipenv combines package management and virtual environment handling into one tool.
pip install pipenvmkdir crawler
cd crawler
pipenv install requestsThis creates two files:
Pipfile– lists project dependenciesPipfile.lock– exact versions used
pipenv --venvpipenv shellNow your terminal is inside the virtual environment.
-
Create a new project folder:
mkdir api_client cd api_client -
Create a virtual environment:
python -m venv .venv source .venv/bin/activate # or .venv\Scripts\activate on Windows
-
Install the
requestslibrary:pip install requests
-
Save requirements:
pip freeze > requirements.txt -
Create
main.py:import requests response = requests.get('https://api.github.com') print(f"Status Code: {response.status_code}")
-
Run the script:
python main.py
-
Exit the environment:
deactivate
💡 Hidden Tips & Notes
- 📁 Never commit the virtual environment folder (e.g.,
.venv) to Git — always commitrequirements.txtorPipfile. - 🧵 Prefer
pipenvorpoetryover manualvenv + requirements.txtfor better dependency management. - 🧩 Always activate the correct environment before running your scripts.
- 📝 Use
pip show <package>to check what a package depends on. - 🔁 Avoid global installations unless necessary — keep them isolated.
- 🧹 Clean up unused packages with
pip uninstall <package>. - 📦 Use
pip cache purgeorpip cache dirto manage downloaded wheels.
| Task | Command |
|---|---|
| Install a package | pip install requests |
| Install specific version | pip install requests==2.32.3 |
| List installed packages | pip list |
| Show package details | pip show requests |
| Upgrade a package | pip install --upgrade requests |
| Uninstall a package | pip uninstall requests |
| Export requirements | pip freeze > requirements.txt |
| Install from requirements | pip install -r requirements.txt |
| Create virtual env | python -m venv .venv |
| Activate env | .venv\Scripts\activate (Windows) / source .venv/bin/activate (Linux/macOS) |
| Deactivate env | deactivate |
| Install pipenv | pip install pipenv |
| Install package with pipenv | pipenv install requests |
| Activate pipenv shell | pipenv shell |
| Topic | Description |
|---|---|
| PyPI | Official repository for third-party Python packages |
| pip | Tool to install, upgrade, and manage packages |
| Semantic Versioning | Format: major.minor.patch |
| Virtual Environments | Isolate dependencies per project using venv |
| pipenv | Combines virtual environment and dependency management |
| Requirements File | Used to reproduce environment across machines |
| Best Practices | Use virtual environments, avoid global installs, document versions |
🎉 Congratulations! You now have a solid understanding of how to manage third-party packages, use pip effectively, and isolate dependencies using virtual environments.
This knowledge empowers you to build robust, scalable, and maintainable Python applications — while avoiding common pitfalls like conflicting dependencies and broken builds.