首页 > 解决方案 > Why won't Google App Engine find my module (Python 3)?

问题描述

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.

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")

标签: pythongoogle-app-engine

解决方案


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.


推荐阅读