Reputation: 293
While trying to add py.test functionality to a Flask API I ran into the following error message when calling py.test on my source directory
E ImportStringError: import_string() failed for 'config'. Possible reasons are:
E
E - missing __init__.py in a package;
E - package or module path not included in sys.path;
E - duplicated package or module name taking precedence in sys.path;
E - missing module, class, function or variable;
E
E Debugged import:
E
E - 'config' not found.
E
E Original exception:
E
E ImportError: No module named config
The issue seems to stem after I have instantiated my Flask app and try to import the config from config.py (line 5).
from flask import Flask
# Setup app
app = Flask(__name__)
app.config.from_object('config')
# Import views
from views import *
if __name__ == '__main__':
app.run()
Everything seems to work accordingly if I manually set the config variables instead of importing them. Has anyone encountered anything like this? I wasn't able to find anything helpful in the documentation.
Upvotes: 14
Views: 21701
Reputation: 1
Just in case it help someone else, removing the empty __init__.py
next to app.py
and config.py
has resolved my issue.
I was able to continue the usage of: app.config.from_object('config')
Upvotes: 0
Reputation: 6162
I suppose these ones are the most probable
E - package or module path not included in sys.path;
E - duplicated package or module name taking precedence in sys.path;
So the first thing I'd try is to rename config file to something like config_default.py
.
Then you can try to use the real object instead of string eg. importing config yourself:
from flask import Flask
import config
# Setup app
app = Flask(__name__)
app.config.from_object(config)
But most likely you'll face the same error: ImportError: No module named config
Also you can use app.config.from_pyfile()
and specify full path to your config file:
app.config.from_pyfile('config.py')
from_pyfile()
uses a different technique to create the object (it uses [exc
](https://github.com/mitsuhiko/flask/blob/master/flask/config.py#L129)).
I myself prefer to use from_envvar()
and specify which config file to use in launcher (supervisord nowadays).
Upvotes: 22
Reputation: 351
Just try absolute import instead
app.config.from_object(app.config)
It worked for me
Upvotes: 1
Reputation: 279
Although this is an old post and very essential problem of Flask, which I happen to have this these days. In case that anyone also have the same problem, you should check the path of config.py file. At first, I created config.py inside app dir. Then I moved outside the app, it works. Like this:
-app---
|
|----__init__.py
|----config.py
Change to:
-app---
|
|----__init__.py
-config.py
Upvotes: 19
Reputation: 426
Changing app.config.from_object(config)
to app.config.from_pyfile('config.py')
solved.
Upvotes: 3
Reputation: 16525
Twil makes good points and alternatives to app.config.from_object. However, I stumbled upon this question while having trouble with the following Flask Tutorial and I wanted to follow the guide and use from_object.
The way I corrected my issue was that I had originally put the config.py file in my 'app' package. It should reside outside of my app in that same folder from which I had my run.py script.
I hope this answer saves someone a bit of time.
Upvotes: 5