Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
76 views
in Technique[技术] by (71.8m points)

python - flask run giving me ModuleNotFoundError

I'm relatively new to python and am trying to build a flask server. What I would like to do is have a package called "endpoints" that has a list of modules where each module defines a subset of application routes. When I make a file called server.py with the following code this works like so

import os

from flask import Flask


app = Flask(__name__)

from endpoint import *

if __name__ == '__main__':
    app.run(debug=True, use_reloader=True)

Right now there's only one endpoint module called hello.py and it looks like this

from __main__ import app


# a simple page that says hello
# @app.route defines the url off of the BASE url e.g. www.appname.com/api + 
#    @app.route
# in dev this will be literally http://localhost:5000/hello
@app.route('/hello')
def hello():
    return 'Hello, World!'

So... the above works when I run python server.py, the issue happens when I try to run the app using flask.

Instead of server.py it just calls __init__.py which looks like this

import os

from flask import Flask

# create and configure the app
# instance_relative_config states that the 
#     config files are relative to the instance folder
app = Flask(__name__, instance_relative_config=True)

# ensure the instance folder exists
try:
    os.makedirs(app.instance_path)
except OSError:
    pass

from endpoint import *

When I run flask run in terminal I get ModuleNotFoundError: No module named 'endpoint'

but again if I change the code so it looks like the following below, then flask run works.

import os

from flask import Flask

# create and configure the app
# instance_relative_config states that the 
#     config files are relative to the instance folder
app = Flask(__name__, instance_relative_config=True)

# ensure the instance folder exists
try:
    os.makedirs(app.instance_path)
except OSError:
    pass

# a simple page that says hello
# @app.route defines the url off of the BASE url e.g. www.appname.com/api + 
#   @app.route
# in dev this will be literally http://localhost:5000/hello
@app.route('/hello')
def hello():
    return 'Hello, World!'

I'm pretty sure this is happening because I don't fully understand how imports work...

So, how do I set up __init__.py so that it imports all the modules from the "endpoint" package and works when I call flask run?

question from:https://stackoverflow.com/questions/65853229/flask-run-giving-me-modulenotfounderror

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When you use a __init__.py file, which you should, Python treats the directory as a package.

This means, you have to import from the package, and not directly from the module.

Also, usually you do not put much or any code in a __init__.py file.

Your directory structure could look like this, where stack is the name of the package I use.

stack/
├── endpoint.py
├── __init__.py
└── main.py

You __init__.py file is empty.

main.py

import os

from flask import Flask

# create and configure the app
# instance_relative_config states that the 
#     config files are relative to the instance folder
app = Flask(__name__, instance_relative_config=True)

# ensure the instance folder exists
try:
    os.makedirs(app.instance_path)
except OSError:
    pass

from stack.endpoint import *

endpoint

from stack.main import app

# a simple page that says hello
# @app.route defines the url off of the BASE url e.g. www.appname.com/api + 
#   @app.route
# in dev this will be literally http://localhost:5000/hello
@app.route('/hello')
def hello():
    return 'Hello, World!'

You can then run your app...

export FLASK_APP=main.py
# followed by a...
flask run

This all said, when I create a new Flask app, I usually only use one file, this makes initial development easier, and only split into modules when the app really grows bigger.

Also, for separating views or let's call it sub packages, Flask offers so called Blue prints. This is nothing you have to worry about right now, but comes especially handy when trying to split the app into sub applications.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...