Why do I need a relative import in Heroku but not when running locally?

Viewed 249

When running locally my Flask app, I can use a common import:

from models import MyModel

But when running on Heroku, I need to change it to:

from .models import MyModel

Why does that happen?

1 Answers

I'm not sure why on Heroku this happens. But it has to do with the Python path.

If you specify the path where the app is on the Procfile, it will work.

Before:

web: gunicorn src.api:api

After:

web: gunicorn src.api:api --pythonpath=./src

Considering that the main file is named api.py and is located inside a folder called src.

Related