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

you can consider this as (Not 100% sure), when you do
flask runthencreate_appfunction is called and runned.it is simply as adding a
run_app.pyfile inprojoutside ofappwhich importcreate_appfunction as as run itso
run_app.pythis will run the server ( as you are doing now)
export FLASK_ENV=apporexport FLASK_ENV=run_app.pyare samenow as you have added config file in
conf.pyand it is working fine for you when that file is inprojfolder not ininstancefolder. This is happening because inapp/__init__.pyfile you have define the location of the config object asconf.DevConfig, so python is looking for this object in proj folder either.since you have define the config object in
instance/conf.pyfile, so python is not able to find it and giving you this error.To solve This, you need to provide the location of the object in
app.config.from_object('conf.DevConfig')so you can do this in two way,
1st. provide full conf object path from the proj folder like
2nd. import the instance in the
app/__init__.pyfile and then provide the config objecteg.
app/__init__.pyNote in method one i am providng object as a string and in 2 as a class method