R
Railway11mo ago
tj

fastapi crash on deploy, univorn start command works locally

Hi team, using railway for the first time, build command is empty, build logs: Using subdirectory "app" ... [succeeds] start command: uvicorn app.server.api:app --host=0.0.0.0 (my dir structure is: /app/server/api.py, which has app = FastAPI() app crashes on error: ModuleNotFoundError: No module named 'app'
44 Replies
Percy
Percy11mo ago
Project ID: 114fa538-776d-4163-b35a-d202c7fa380d
tj
tjOP11mo ago
114fa538-776d-4163-b35a-d202c7fa380d
Adam
Adam11mo ago
when you run your start command locally, what folder are you in?
tj
tjOP11mo ago
root wondering if it's because build uses app/ subdir, that the command should be relative to /app/?
Adam
Adam11mo ago
you got it, try launching server.api:app instead
tj
tjOP11mo ago
so railway infers the project dir for build? thanks will try now
Adam
Adam11mo ago
Railway copies your project files into a folder called /app/ so if you have a /app folder that is not your root, the structure should be /app/app/server/app etc but it sounds to me like your /app is set to your root folder
tj
tjOP11mo ago
still get:
ModuleNotFoundError: No module named 'app'
ModuleNotFoundError: No module named 'app'
in settings I left "root dir" field empty let me try uvicorn app.app.server.api:app next?
Adam
Adam11mo ago
can you share your repo structure? sure try that out
tj
tjOP11mo ago
api.py has
app = FastAPI()
app = FastAPI()
Adam
Adam11mo ago
hmm app/server/api should work… Let me know if app/app/.. works
Brody
Brody11mo ago
looks like they have their entire project located within a sub folder, maybe you could try setting the root directory to /app in the service settings, and then go back to the start command that works locally
tj
tjOP11mo ago
I think I started with this, but will try again
Adam
Adam11mo ago
actually, looks to me like app/server/ should be your root dir. your requirements.txt is in there And then change the start command to server/api:
Brody
Brody11mo ago
yeah either way this is a unorthodox project structure
tj
tjOP11mo ago
actually I think req.txt is in app/
Adam
Adam11mo ago
yep you’re right, the tab spacing is hard to read app it is then
tj
tjOP11mo ago
would it be easier if I just unnest the /app layer?
Adam
Adam11mo ago
yeah, railway will be able to read it for itself then and you won’t have to specify a start command
Brody
Brody11mo ago
your manage.py being within server is strange, it should be at the root of your project (or wherever req.txt is)
tj
tjOP11mo ago
oh this manager.py is business logic related, not a server config lookslike setting /app as root and modifying command also crashed, so I'll try unnesting project structure oh it's fastapi haha
Adam
Adam11mo ago
yeah brody realized that and deleted lol
tj
tjOP11mo ago
love django too 😁
Adam
Adam11mo ago
I’m a flask user, personally django apis are too complicated
tj
tjOP11mo ago
so what should I try next?
Adam
Adam11mo ago
did unnesting the project structure work? does the error trace back to a specific line?
tj
tjOP11mo ago
as for errors, I noticed it's using python 3.8 (I'm using 3.12)
Adam
Adam11mo ago
I don’t think 12 is supported atm
Brody
Brody11mo ago
sorry adam
Adam
Adam11mo ago
hey rude
Brody
Brody11mo ago
https://nixpacks.com/docs/providers/python
Setting the version in a runtime.txt file
Adam
Adam11mo ago
ahh yeah that’ll be better still don’t know if 12 is supported though
Brody
Brody11mo ago
its not 😦 but 3.11 should be sufficient
tj
tjOP11mo ago
can try 3.11, didn't use any 3.12 exclusives
Adam
Adam11mo ago
11 should fix any issues, unless you’re using any 12 specific syntax like switch statements (don’t know what I’m on, switch statements are 3.10)
tj
tjOP11mo ago
is it just this? where runtime.txt is at project root
Brody
Brody11mo ago
that should work, but it really only needs to be 3.11
tj
tjOP11mo ago
gotcha, trying now
Adam
Adam11mo ago
pretty sure it should be python-3.11, but we’ll see not sure about not having python there
Brody
Brody11mo ago
I think the matcher is just regex looking for some numbers, so either works
tj
tjOP11mo ago
btw this was resolved, thanks! I forgot how to mark it as so, please let me know if that's a feature actuallly quick followup, deploy logs say: INFO: Started server process [7] INFO: Waiting for application startup. INFO: Application startup complete. INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit) however visiting the site: https://agency-brain-production.up.railway.app/ just gives: in dev, http://localhost:8000/ gives "hello world"
tj
tjOP11mo ago
works like a charm, thanks a bunch guys!
Brody
Brody11mo ago
no problem
Want results from more Discord servers?
Add your server