Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

py.test import error "- 'config' not found."

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.

like image 951
user1558914 Avatar asked Dec 21 '13 21:12

user1558914


People also ask

How do you fix an import error in Python?

Python's ImportError ( ModuleNotFoundError ) indicates that you tried to import a module that Python doesn't find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH .

Where do I put Pytest files?

While the pytest discovery mechanism can find tests anywhere, pytests must be placed into separate directories from the product code packages. These directories may either be under the project root or under the Python package.

How do I run a specific test in Pytest?

Running pytest We can run a specific test file by giving its name as an argument. A specific function can be run by providing its name after the :: characters. Markers can be used to group tests. A marked grouped of tests is then run with pytest -m .


4 Answers

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
like image 158
CrackThisRay Avatar answered Oct 22 '22 01:10

CrackThisRay


Changing app.config.from_object(config) to app.config.from_pyfile('config.py') solved.

like image 23
David Avatar answered Oct 17 '22 23:10

David


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).

like image 22
twil Avatar answered Oct 22 '22 00:10

twil


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.

like image 5
ThinkBonobo Avatar answered Oct 22 '22 00:10

ThinkBonobo