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 run
thencreate_app
function is called and runned.it is simply as adding a
run_app.py
file inproj
outside ofapp
which importcreate_app
function as as run itso
run_app.py
this will run the server ( as you are doing now)
export FLASK_ENV=app
orexport FLASK_ENV=run_app.py
are samenow as you have added config file in
conf.py
and it is working fine for you when that file is inproj
folder not ininstance
folder. This is happening because inapp/__init__.py
file 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.py
file, 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__.py
file and then provide the config objecteg.
app/__init__.py
Note in method one i am providng object as a string and in 2 as a class method