8000 python-cheatsheet/Section_15_pip_package at main · onyxwizard/python-cheatsheet · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

🛠️ Section 15: Third-party Packages, PIP, and Virtual Environments

📦 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 pip for installing, upgrading, listing, and uninstalling packages
  • 🧱 Managing package versions with semantic versioning
  • 🧩 Working with virtual environments using venv and pipenv
  • 💡 Best practices and hidden tips for managing dependencies effectively

🧠 What You'll Learn

  • 📦 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, and pip show to inspect installed packages
  • 🧪 Create and manage isolated virtual environments using venv and pipenv
  • 💡 Hidden notes on avoiding dependency conflicts, managing requirements, and setting up reproducible environments

📦 Introduction to Python Package Index (PyPI)

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.

🔎 How to Search for a Package

  1. Go to https://pypi.org
  2. Use the search bar to find relevant packages.
  3. Click on the package name to view its documentation, version history, and installation instructions.

🔸 Popular packages include:

  • requests – HTTP requests made simple
  • numpy – Numerical computing
  • pandas – Data manipulation
  • flask – Lightweight web framework

🧰 Using pip – The Python Installer

pip is the default package manager for Python and comes pre-installed with Python 3.4+.

✅ Check if pip Is Installed

pip --version

If not available, you can usually install it via:

python -m ensurepip --upgrade

📥 Install a Package

pip install requests

Install a specific version:

pip install requests==2.20.1

🔄 Upgrade a Package

pip install --upgrade requests

📋 List Installed Packages

pip list

To check outdated packages:

pip list --outdated

📦 Show Package Details

pip show requests

Includes info like:

  • Version
  • Summary
  • Author
  • License
  • Required dependencies

❌ Uninstall a Package

pip uninstall requests

You’ll be prompted to confirm before removal.

🧹 Freeze Requirements for Reproducibility

Use pip freeze to export all installed packages and their exact versions:

pip freeze > requirements.txt

This 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.txt

📏 Semantic Versioning Explained

Packages 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 update
  • requests==2.33.0 – minor update
  • requests==3.0.0 – major update (may break existing code)

🧪 Why You Need Virtual Environments

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.

📁 Creating Virtual Environments with venv

Python has built-in support for virtual environments via the venv module (available since Python 3.3).

✅ Create a New Environment

mkdir myproject
cd myproject
python -m venv .venv

🔌 Activate the Environment

On Windows:

.venv\Scripts\activate

On macOS/Linux:

source .venv/bin/activate

You’ll see something like:

(.venv) C:\myproject>

🚫 Deactivate When Done

deactivate

📦 Managing Dependencies in a Virtual Environment

Once activated, any package installed will be local to that environment.

✅ Install a Package in Your Virtual Environment

pip install requests

📄 Export Requirements

pip freeze > requirements.txt

🧬 Reinstall Dependencies Later

pip install -r requirements.txt

🧱 Alternative: pipenv – Modern Packaging Tool

pipenv combines package management and virtual environment handling into one tool.

📥 Install pipenv

pip install pipenv

✅ Create a New Project

mkdir crawler
cd crawler
pipenv install requests

This creates two files:

  • Pipfile – lists project dependencies
  • Pipfile.lock – exact versions used

🔍 Check Where the Environment Lives

pipenv --venv

🧪 Activate the Shell

pipenv shell

Now your terminal is inside the virtual environment.

🧩 Real-World Example – Building an API Client

🗂️ Step-by-step:

  1. Create a new project folder:

    mkdir api_client
    cd api_client
  2. Create a virtual environment:

    python -m venv .venv
    source .venv/bin/activate  # or .venv\Scripts\activate on Windows
  3. Install the requests library:

    pip install requests
  4. Save requirements:

    pip freeze > requirements.txt
  5. Create main.py:

    import requests
    
    response = requests.get('https://api.github.com')
    print(f"Status Code: {response.status_code}")
  6. Run the script:

    python main.py
  7. Exit the environment:

    deactivate

💡 Hidden Tips & Notes

  • 📁 Never commit the virtual environment folder (e.g., .venv) to Git — always commit requirements.txt or Pipfile.
  • 🧵 Prefer pipenv or poetry over manual venv + requirements.txt for 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 purge or pip cache dir to manage downloaded wheels.

🧪 Common Commands Reference

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

📌 Summary

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.

0