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

# Installing Python Packages with pip and PyPI Guide

> Learn how to use pip to install, update, and remove Python packages from PyPI, manage requirements.txt, and avoid common installation pitfalls.

One of Python's greatest strengths is the enormous ecosystem of open-source packages available for almost every task imaginable. Instead of writing complex code from scratch to download web data, process spreadsheets, or call an AI API, you can install a package written and maintained by other developers and use it in your project immediately. Understanding how to manage these packages with `pip` is one of the most practical skills you will use every day as a Python developer.

## What are packages?

Packages are collections of Python code organised to solve specific problems. They are distributed through [PyPI](https://pypi.org/) — the Python Package Index — which hosts over 500,000 packages.

Some packages you will use frequently in this course:

| Package         | What it does                                           |
| --------------- | ------------------------------------------------------ |
| `requests`      | Sends HTTP requests to download web data and call APIs |
| `pandas`        | Reads, cleans, and analyses tabular data               |
| `numpy`         | Fast numerical operations and array manipulation       |
| `openai`        | Connects to OpenAI's AI models                         |
| `python-dotenv` | Loads environment variables from `.env` files          |
| `ipykernel`     | Enables interactive Python execution in VS Code        |

## Meet pip

`pip` (Pip Installs Packages) is Python's built-in package manager. It comes with Python and is already available inside your virtual environment. It handles downloading packages from PyPI, installing them, resolving their dependencies, and managing versions.

<Note>
  When you activate your virtual environment, you get a dedicated copy of pip that installs packages only into that environment. Packages installed here do not affect any other project or your system Python.
</Note>

## Install your first package

<Steps>
  <Step title="Open the VS Code terminal">
    Press `` Ctrl + ` `` (Windows/Linux) or `` Cmd + ` `` (macOS). Confirm you see `(.venv)` at the start of the prompt — this means your virtual environment is active.
  </Step>

  <Step title="Install a package">
    ```bash theme={null}
    pip install requests
    ```

    You will see output like:

    ```
    Collecting requests
      Downloading requests-2.31.0-py3-none-any.whl (62 kB)
    Installing collected packages: requests
    Successfully installed requests-2.31.0
    ```
  </Step>

  <Step title="Use it in your code">
    ```python theme={null}
    import requests

    response = requests.get("https://api.github.com")
    print(response.status_code)  # 200 means success
    ```
  </Step>
</Steps>

Notice that installing `requests` also installed packages like `certifi`, `charset-normalizer`, and `urllib3`. These are **dependencies** — packages that `requests` itself relies on. pip resolves and installs the full dependency tree automatically.

<Tip>
  To see the actual files that were installed, look inside `.venv/lib/python3.x/site-packages/requests/` — it is just Python files that someone else wrote and shared.
</Tip>

## Common pip commands

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

# Install multiple packages at once
pip install requests pandas numpy

# Install a specific version
pip install requests==2.30.0

# Install minimum version
pip install requests>=2.28.0

# Upgrade an installed package
pip install --upgrade requests

# Uninstall a package
pip uninstall requests

# List all installed packages
pip list

# Show details about a specific package
pip show requests
```

## Managing dependencies with requirements.txt

When you share your project with others (or deploy it to a server), they need to know which packages to install and which versions you used. A `requirements.txt` file captures this information.

**Generate a requirements file:**

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

The file looks like this:

```
certifi==2024.2.2
charset-normalizer==3.3.2
idna==3.6
requests==2.31.0
urllib3==2.2.0
```

**Install from a requirements file:**

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

This is the standard way to reproduce an environment — on a new machine, in a team, or in a CI/CD pipeline. Anyone with your `requirements.txt` can recreate an identical environment with a single command.

<Tip>
  Commit your `requirements.txt` to version control (Git) so your teammates and future deployments always have a record of the exact package versions your project depends on.
</Tip>

## Troubleshooting

<AccordionGroup>
  <Accordion title="pip: command not found" icon="circle-exclamation">
    Your virtual environment is probably not active. Check whether `(.venv)` appears in your terminal prompt. If not, select your interpreter in VS Code via `Ctrl/Cmd + Shift + P` → **Python: Select Interpreter**.
  </Accordion>

  <Accordion title="Permission denied" icon="lock">
    You are probably trying to install into system Python rather than your virtual environment. Never use `sudo pip install`. Always confirm `(.venv)` is visible in your prompt before installing.
  </Accordion>

  <Accordion title="Package not found" icon="ban">
    Package names on PyPI can differ from what you might expect:

    * "Beautiful Soup" → `pip install beautifulsoup4`
    * "OpenAI" → `pip install openai`
    * "Pillow (PIL)" → `pip install pillow`

    Search [pypi.org](https://pypi.org) for the exact package name if you are unsure.
  </Accordion>

  <Accordion title="Version conflict errors" icon="triangle-exclamation">
    If pip reports a conflict between package versions, the cleanest solution is to create a fresh virtual environment and reinstall only what your project needs. This is exactly why you use one virtual environment per project.
  </Accordion>
</AccordionGroup>

<Card title="Interactive Python" icon="sparkles" href="/getting-started/interactive-python">
  Learn how to run Python interactively from a .py file using Shift + Enter.
</Card>
