> ## Documentation Index
> Fetch the complete documentation index at: https://python4ai.codewithsiva.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Working with External Modules

> Learn to install, manage, and use third-party packages using pip, PyPI, and uv

While Python's built-in standard library is incredibly powerful, the real superpower of Python is its massive ecosystem of third-party libraries. These are packages written by the open-source community to solve almost any programming task imaginable.

## What is PyPI?

**PyPI** (pronounced *pie-pee-eye*), or the **Python Package Index**, is the official online repository for third-party Python software.

* Anyone can write a Python package and publish it to PyPI.
* When you need a package (like `pandas` or `requests`), you download it directly from PyPI.
* Browse packages at [https://pypi.org](https://pypi.org).

## What is pip?

**pip** is the standard package manager for Python. It comes pre-installed with modern Python installations and allows you to install and manage packages directly from PyPI.

## `requests` Module

The **`requests`** module is a popular third-party Python library used to send HTTP requests and communicate with REST APIs.

Install it using:

```bash theme={null}
pip install requests
```

### Common `pip` Commands

```bash theme={null}
# Install the latest version
pip install requests

# Install a specific version
pip install requests==2.28.0

# Upgrade a package
pip install --upgrade requests

# Uninstall a package
pip uninstall requests

# List installed packages
pip list
```

<Tip>
  **Important:** Activate your virtual environment before installing packages to avoid installing them globally.

  Learn more about <a href="/getting-started/virtual-environments">Virtual Environments</a>.
</Tip>

## What is `uv`?

**uv** is a modern, ultra-fast Python package manager written in Rust. It is designed as a replacement for `pip`, `pip-tools`, and `virtualenv`.

### Why use `uv`?

* 🚀 10–100× faster than `pip`
* 📦 Manages virtual environments
* 🐍 Can install Python interpreters
* ⚡ Uses aggressive dependency caching

### Common `uv` Commands

```bash theme={null}
# Install a package
uv pip install requests

# Create a virtual environment
uv venv

# Install dependencies
uv pip install -r requirements.txt
```

## Sharing Dependencies (`requirements.txt`)

Instead of sharing your `.venv` folder, share a **requirements.txt** file containing your project's dependencies.

### Generate the file

```bash theme={null}
# Using pip
pip freeze > requirements.txt

# Using uv
uv pip freeze > requirements.txt
```

Example:

```text theme={null}
certifi==2024.2.2
charset-normalizer==3.3.2
requests==2.31.0
urllib3==2.2.0
```

### Install dependencies

```bash theme={null}
# Using pip
pip install -r requirements.txt

# Using uv
uv pip install -r requirements.txt
```

## Using External Packages

```python theme={null}
import requests

response = requests.get("https://api.github.com/events")

print(response.status_code)
print(response.json())
```

## Common Mistakes

<AccordionGroup>
  <Accordion title="ModuleNotFoundError">
    Python cannot find the package.

    **Common causes**

    1. The package is not installed.
    2. The virtual environment is not activated.
    3. VS Code is using a different Python interpreter.
  </Accordion>

  <Accordion title="Name Conflicts">
    Don't name your script the same as a package.

    **Example:** `requests.py`

    Python will import your file instead of the real `requests` package.
  </Accordion>
</AccordionGroup>

## Practice & Exercises

<CardGroup cols={2}>
  <Card title="Follow-Along Practice" icon="laptop-code">
    Practice installing packages, importing modules, and working with the `requests` library.

    [💻 VS Code](vscode://file/Users/sivaprasad/Downloads/python%20material/python4ai/public/notebooks/basics_exercises/External_Modules_Practice.ipynb) |
    [🚀 Colab](https://colab.research.google.com/github/prasad230776/python4ai/blob/master/public/notebooks/basics_exercises/External_Modules_Practice.ipynb) |
    <a href="/public/notebooks/basics_exercises/External_Modules_Practice.ipynb" download>📥 Download</a>
  </Card>

  <Card title="Practice Exercises" icon="pen-to-square">
    Solve exercises on package installation, imports, and API requests.

    [💻 VS Code](vscode://file/Users/sivaprasad/Downloads/python%20material/python4ai/public/notebooks/basics_exercises/External_Modules_Exercises.ipynb) |
    [🚀 Colab](https://colab.research.google.com/github/prasad230776/python4ai/blob/master/public/notebooks/basics_exercises/External_Modules_Exercises.ipynb) |
    <a href="/public/notebooks/basics_exercises/External_Modules_Exercises.ipynb" download>📥 Download</a>
  </Card>
</CardGroup>

## What's Next?

<Card title="Working with APIs" icon="arrow-right" href="/libraries-apis/working-with-apis">
  Connect to online services using the `requests` library.
</Card>
