Update: with a lot of messing around with imports, directory structure, things have moved forward. New questions arise, and I'll move that to a new question. Thanks for the input.
I'm porting a Python library to run as a service in Flask. New to Python/Flask as far as serving remotely goes. I am set up with a Github repo that I manually deploy to Heroku.
Deploying reports no errors and says it built fine. But going to the site, where I expect to encounter the output of the base route, I get a failure, and the logs look like this:
2021-01-29T06:25:50.732453+00:00 app[web.1]: mod = importlib.import_module(module)
2021-01-29T06:25:50.732453+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
2021-01-29T06:25:50.732454+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2021-01-29T06:25:50.732454+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2021-01-29T06:25:50.732455+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2021-01-29T06:25:50.732455+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
2021-01-29T06:25:50.732455+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
2021-01-29T06:25:50.732456+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 678, in exec_module
2021-01-29T06:25:50.732456+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2021-01-29T06:25:50.732456+00:00 app[web.1]: File "/app/wsgi.py", line 1, in <module>
2021-01-29T06:25:50.732456+00:00 app[web.1]: from app.main import main
2021-01-29T06:25:50.732457+00:00 app[web.1]: File "/app/app/main.py", line 1, in <module>
2021-01-29T06:25:50.732457+00:00 app[web.1]: from game import Game
2021-01-29T06:25:50.732464+00:00 app[web.1]: ModuleNotFoundError: No module named 'game'
2021-01-29T06:25:50.732632+00:00 app[web.1]: [2021-01-29 06:25:50 +0000] [9] [INFO] Worker exiting (pid: 9)
2021-01-29T06:25:50.814394+00:00 app[web.1]: [2021-01-29 06:25:50 +0000] [10] [INFO] Booting worker with pid: 10
2021-01-29T06:25:50.857735+00:00 app[web.1]: [2021-01-29 06:25:50 +0000] [10] [ERROR] Exception in worker process
So, it appears that main.py, which is trying to import Game is not finding it. This is specific to this environment. Running it via the terminal produced no problems. This is the structure of the project:
Not visible: wsgi.py, Procfile and requirements.txt are all at the same level as app/. Again, the build seems to go fine, and I would assume that I don't understand how the path to game.py is speficied in this situation.
To keep Heroku happy, the directory structure has been modified: I created the app directory and put all the project code into it, so the relative locations of main.py
and game.py
remain the same, but down in the project hierarchy a level. Is this the problem?
Finally, here's main.py:
from game import Game
from flask import Flask
from gunicorn import Gunicorn
app = Flask(__name__)
@app.route('/')
def main():
game = Game()
print(game.bleep())
return dict(plant=game.bleep())
the calls to game.bleep() are just testcode to ensure that main is calling a method in the game class. Running on Flask, on localhost, and before the reorganization of the directory structure, this worked.
I have a feeling this is obvious, but of course I'd be delighted if it was really obscure, so I could continue to feel intelligent. Not optimistic about that.
question from:
https://stackoverflow.com/questions/65949944/flask-heroku-relative-path-to-modules-called-by-main