# Fix FastAPI ModuleNotFoundError in systemd Service

## Problem
The systemd service is using `/usr/bin/python3` (Python 3.8) but FastAPI is installed in the user's pyenv environment (Python 3.10).

## Solution Options

### Option 1: Install FastAPI for System Python (Recommended)

Run these commands:

```bash
# Install all dependencies for system Python (runs as root)
cd /var/www/html/live_calls/homebook
sudo /usr/bin/python3 -m pip install -r requirements.txt

# Verify installation
sudo /usr/bin/python3 -c "import fastapi; print('FastAPI:', fastapi.__version__)"

# Restart the service
sudo systemctl restart websocket_api.service

# Check status
sudo systemctl status websocket_api.service
```

### Option 2: Update systemd Service to Use pyenv Python

Edit the service file:

```bash
sudo nano /etc/systemd/system/websocket_api.service
```

Change the ExecStart line to use pyenv Python:

```ini
[Service]
Type=simple
WorkingDirectory=/var/www/html/live_calls/homebook
ExecStart=/home/vmc/.pyenv/shims/python3 /var/www/html/live_calls/homebook/main.py
User=vmc
Group=vmc
Restart=always
RestartSec=5
```

Then reload and restart:

```bash
sudo systemctl daemon-reload
sudo systemctl restart websocket_api.service
```

### Option 3: Use Virtual Environment (Best Practice)

```bash
# Create virtual environment
cd /var/www/html/live_calls/homebook
python3 -m venv venv

# Activate and install dependencies
source venv/bin/activate
pip install -r requirements.txt

# Update systemd service
sudo nano /etc/systemd/system/websocket_api.service
```

Change ExecStart to:

```ini
ExecStart=/var/www/html/live_calls/homebook/venv/bin/python3 /var/www/html/live_calls/homebook/main.py
```

Then:

```bash
sudo systemctl daemon-reload
sudo systemctl restart websocket_api.service
```

## Quick Fix (Run These Commands)

```bash
# 1. Install FastAPI for system Python
cd /var/www/html/live_calls/homebook
sudo /usr/bin/python3 -m pip install fastapi uvicorn[standard] websockets python-dotenv pydantic starlette

# 2. Install other required packages
sudo /usr/bin/python3 -m pip install mysql-connector-python aiohttp requests psutil numpy pydub

# 3. Verify
sudo /usr/bin/python3 -c "from fastapi import FastAPI; print('✓ FastAPI installed')"

# 4. Restart service
sudo systemctl restart websocket_api.service

# 5. Check logs
sudo journalctl -u websocket_api.service -n 20 --no-pager
```

## Verify the Fix

```bash
# Check service status
sudo systemctl status websocket_api.service

# Check if it's running
sudo systemctl is-active websocket_api.service

# View recent logs
sudo journalctl -u websocket_api.service -f
```

## Current Service Configuration

The service file is at: `/etc/systemd/system/websocket_api.service`

Current settings:
- Python: `/usr/bin/python3` (Python 3.8.10)
- User: root
- Working Directory: `/var/www/html/live_calls/homebook`

## Recommended: Option 1 (Install for System Python)

This is the simplest fix - just install the packages system-wide:

```bash
cd /var/www/html/live_calls/homebook
sudo /usr/bin/python3 -m pip install -r requirements.txt
sudo systemctl restart websocket_api.service
```

