1
votes

I'm new to Google App Engine and having the trouble that the app doesn't find my module. I get the error line 5, in <module> import foo as bar ModuleNotFoundError: No module named 'foo'. I have the current file structure as seen below (following a great tutorial for Flask).

The problem is that routes.py cannot import foo.py.

  • app engine:/
    • app
      • static/css
      • templates
      • __init__.py
      • foo.py
      • routes.py
    • app.yaml
    • config.py
    • main.py
    • requirements.txt
    • source-context.json

Why is this? Are there special requirements for how files are structured on App Engine as this works locally?

Also, just to have things working I've tried having the code in the module foo in routes instead and the code works. But the code doesn't belong there and I want to structure it better but the app breaks when separating. In the end I would like to add directory "app engine":/app/libs (or else on recomendation) where I store my custom stuff.

EDIT (add code sample from routes.py)

from flask import render_template, flash, redirect, url_for
from app import app
from app.forms import LookupForm
import logging
import foo as bar

@app.route("/")
@app.route("/index")

def index():
    return render_template("index.html")
1
Could you add a minimal code sample displaying how you are performing the import? - Deniss Tsokarev
@DenisT. Added top of code from routes.py. - Nelumbo
@Nelumbo I would suggest you to follow GCP structure of an app project : cloud.google.com/appengine/docs/standard/python/getting-started/… - Kaustubh Ghole
@Nelumbo Also you can refer how to structure your web service files: cloud.google.com/appengine/docs/standard/python3/building-app/… - Kaustubh Ghole

1 Answers

3
votes

I was able to reproduce the error you are experiencing. Here are my observations:

  • You are storing the foo module in a local folder called 'app' (a sub-directory of where you have your main.py file).
  • In order to reference the module in this situation, you would need to include the name of the sub-directory when doing the import.

Change the following line in your routes.py file:

import foo as bar

to:

import app.foo as bar

I have tested this solution and it worked for me. Please let me know if it helps.