Import code from parent directory
I have 2 railway services:
api
task_queue
Both are written in Python and are the replications of my monorepo structure - 2 directories in a single repository.
task_queue uses some code which is defined in api (which is also in api directory, so I can't just move the code there), the way I do that in task_queue is by injecting root path in sys.path, that way I can import the code from api. Works well on my local but doesn't work in Railway - I get ModuleNotFoundError during the deployment stage.
I have root paths defined for both services to /api and /task_queue, respectively.
How can I use the code defined in /api inside of /task_queue?
Any help would be appreciated
4 Replies
Project ID:
N/A
How can I use the code defined in /api inside of /task_queue?you can not use code outside of the set root directory, you would have to remove the root directory settings
Thanks @Brody, that makes sense.
I've removed the root path and as a result Python is not detected as a provider (as expected).
I've added providers explicitly in railway.json file.
I've also added custom build command since requirements.txt is going to be nested in the child directory.
This is how my railway.json looks like:
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS",
"buildCommand": "pip install -r job_queue/requirements.txt",
"nixpacksPlan": {
"providers": ["python"]
}
},
"deploy": {
"startCommand": "celery -A job_queue.worker.app worker --loglevel=INFO",
"restartPolicyType": "ON_FAILURE",
"restartPolicyMaxRetries": 3
}
}
However build still fails due to:
0.291 /bin/bash: line 1: pip: command not found
Could you help me out with this one as well? should I move this to nixpacks.toml file? if so which field would be required there to be added?
I think this would best be done with a Dockerfile, please look into how you would implement that