Skip to content

Commit 9bff2ec

Browse files
TamTunnelTamTunnel
authored andcommitted
Fix fragile sys.path hacks in backend services
- Replaces hardcoded string replacement in main.py with robust pathlib logic - Fixes potential startup crash in Docker where file paths don't match source structure - Handles imports via PYTHONPATH (Docker default) or local relative path fallback
1 parent e293c06 commit 9bff2ec

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

backend/ai-agents/app/main.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@
77
from fastapi import FastAPI, Request
88
from fastapi.middleware.cors import CORSMiddleware
99

10-
# Add common module to path
11-
sys.path.insert(0, str(__file__).replace('/ai-agents/app/main.py', ''))
10+
# Add common module to path (fallback for local run, handled by PYTHONPATH in Docker)
11+
try:
12+
import common
13+
except ImportError:
14+
import sys
15+
from pathlib import Path
16+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent))
1217

1318
from .routes import router
1419

backend/core-orbits/app/main.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
from fastapi import FastAPI, Request
99
from fastapi.middleware.cors import CORSMiddleware
1010

11-
# Add common module to path
12-
sys.path.insert(0, str(__file__).replace('/core-orbits/app/main.py', ''))
11+
# Add common module to path (fallback for local run, handled by PYTHONPATH in Docker)
12+
try:
13+
import common
14+
except ImportError:
15+
import sys
16+
from pathlib import Path
17+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent))
1318

1419
from .routes import router
1520
from .demo_routes import router as demo_router

backend/ground-scheduler/app/main.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@
77
from fastapi import FastAPI, Request
88
from fastapi.middleware.cors import CORSMiddleware
99

10-
# Add common module to path
11-
sys.path.insert(0, str(__file__).replace('/ground-scheduler/app/main.py', ''))
10+
# Add common module to path (fallback for local run, handled by PYTHONPATH in Docker)
11+
try:
12+
import common
13+
except ImportError:
14+
import sys
15+
from pathlib import Path
16+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent))
1217

1318
from .routes import router
1419
from .db import init_db, get_db

backend/routing/app/main.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
from fastapi import FastAPI, Request
99
from fastapi.middleware.cors import CORSMiddleware
1010

11-
# Add common module to path
12-
sys.path.insert(0, str(__file__).replace('/routing/app/main.py', ''))
11+
# Add common module to path (fallback for local run, handled by PYTHONPATH in Docker)
12+
try:
13+
import common
14+
except ImportError:
15+
import sys
16+
from pathlib import Path
17+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent))
1318

1419
from .routes import router
1520
from .db import init_db, get_db

0 commit comments

Comments
 (0)