I've been trying to solve this for a couple days now. I'm running flask using the application setup as described in the tutorial. Below is are the packages installed in the virtual environment.
pip3 freeze
click==7.1.2 Flask==1.1.2 itsdangerous==1.1.0 Jinja2==2.11.2 MarkupSafe==1.1.1 pkg-resources==0.0.0 python-dotenv==0.15.0 Werkzeug==1.0.1
I have the following in a .env file:
FLASK_APP=myapp
just so I can do flask run
. My directory structure looks like:
This is all contained within a directory called 'proj'
init.py
import os
from flask import Flask
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
app.config.from_object('conf.DevConfig')
app.config.from_mapping(DATABASE=os.path.join(app.instance_path, 'tester.sqlite'))
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
# a simple page that says hello
@app.route('/hello')
def hello():
return 'Hello, World!'
return app
in proj I run flask run
and get the following:
werkzeug.utils.ImportStringError: import_string() failed for 'conf.DevConfig'. Possible reasons are:
- missing __init__.py in a package;
- package or module path not included in sys.path;
- duplicated package or module name taking precedence in sys.path;
- missing module, class, function or variable;
Debugged import:
- 'conf' not found.
Original exception:
ModuleNotFoundError: No module named 'conf'
Notice that there is a conf.py in proj/instance/
conf.py contains
class Config(object):
DATABASE = 'tester.sqlite'
class DevConfig(Config):
DEBUG = True
class ProdConfig(Config):
DEBUG = False
Oddly enough, if I put conf.py in proj/ then the application loads just fine. I could have swore I read that Flask will search proj/instance by default for files. Why does it find it when I move it one level back from instance. For that matter can .env files be stored in instance and will flask auto find them? To me it seems like instance_relative_config=True
isn't doing what it should be doing. What effect does calling flask run
have if it is run in proj/ vs proj/myapp