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

# Python

> Python preset with pip, venv, and systemd process management. Versions 3.11 and 3.12 (default) available.

The Python preset provides Python with pip and a virtual environment, managed by a systemd service.

## Available Versions

| Version         | Status    |
| --------------- | --------- |
| **Python 3.11** | Supported |
| **Python 3.12** | Default   |

Select a version when creating your pod. If not specified, Python 3.12 is used.

```bash theme={null}
instapods pods create my-app -p python --version 3.11
```

## What's Included

| Component   | Version                    | Details                            |
| ----------- | -------------------------- | ---------------------------------- |
| **Python**  | 3.11 or 3.12 (your choice) | Python runtime                     |
| **pip**     | Bundled                    | Package manager                    |
| **venv**    | Built-in                   | Virtual environment (auto-created) |
| **systemd** | —                          | Process manager (`app` service)    |
| **git**     | Latest                     | Version control                    |
| **SSH**     | OpenSSH                    | Access via dedicated port          |

Pre-installed utilities: curl, wget, vim, htop, unzip.

## Directory Structure

```
/home/instapod/app/
├── requirements.txt
├── app.py              # Entry point
└── venv/               # Virtual environment (auto-created)
```

* **App Root**: `/home/instapod/app`
* **App Port**: 8000 (your app must listen on this port)
* **Virtual environment**: `/home/instapod/app/venv`

## How the App Service Works

Your Python app runs as a systemd service called `app`. It:

* Runs `/home/instapod/app/venv/bin/python app.py` from the app directory
* Starts automatically on boot
* Restarts on crash (`on-failure` policy)
* Logs to the system journal
* Runs as the `instapod` user

The service expects your app to listen on port **8000**. Traffic from your pod's public URL is proxied to this port.

<Warning>
  Your app **must** bind to `0.0.0.0` (not `localhost` or `127.0.0.1`) on port 8000, otherwise it won't be reachable from the public URL.
</Warning>

## Virtual Environment

A Python virtual environment is automatically created at `/home/instapod/app/venv` when the pod is provisioned. It is also auto-activated on SSH and terminal login — you can use `pip` directly without activating it manually.

```bash theme={null}
# These all work inside the pod
pip install flask
python app.py
```

## Deploying Your App

### Upload and Reload

```bash theme={null}
# Sync your project
instapods files sync my-python-app --local ./my-project

# Reload (auto-installs deps + restarts)
instapods pods reload my-python-app
```

<Tip>
  `pods reload` automatically runs `pip install -r requirements.txt` (using the venv) when it detects a `requirements.txt` in your app directory. You don't need to install dependencies manually.
</Tip>

### Example Flask App

```python theme={null}
# app.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello from InstaPods!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)
```

```
# requirements.txt
flask>=3.0
gunicorn>=21.2
```

### Example FastAPI App

```python theme={null}
# app.py
from fastapi import FastAPI
import uvicorn

app = FastAPI()

@app.get('/')
def hello():
    return {'message': 'Hello from InstaPods!'}

if __name__ == '__main__':
    uvicorn.run(app, host='0.0.0.0', port=8000)
```

## Gunicorn Auto-Detection

If `gunicorn` is installed in your virtual environment (listed in `requirements.txt`), `pods reload` automatically configures the systemd service to use gunicorn instead of `python app.py`:

```
gunicorn -b 0.0.0.0:8000 app:app
```

If a `wsgi.py` file exists in your app directory, it uses `wsgi:app` as the module instead. This works out of the box for Django projects.

## Viewing Logs

```bash theme={null}
instapods logs my-python-app -s app
```

## Adding a Database

```bash theme={null}
instapods services add my-python-app -s postgresql -w
instapods services creds my-python-app -s postgresql
```

<Note>
  Database services require the Build plan or higher.
</Note>

## Use Cases

* Django applications
* Flask / FastAPI APIs
* Data science dashboards (Streamlit, Dash)
* Machine learning model serving
* Python scripts and automation
