ImportError: No module named config on Travis-CI build

I have an import error on Travis build , the error is related to the flask config:

from flask import Flask

app = Flask(__name__)
app.config.from_object('config')

      

On the local machine, the flask application works correctly. But on travis here is the error trace

$ nosetests --with-coverage --cover-package=core
E.........................
======================================================================
ERROR: Failure: ImportStringError (import_string() failed for 'config'. 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:
- 'config' not found.
Original exception:
ImportError: No module named config)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/travis/virtualenv/python2.7_with_system_site_packages/local/lib/python2.7/site-packages/nose/loader.py", line 414, in loadTestsFromName
addr.filename, addr.module)
  File "/home/travis/virtualenv/python2.7_with_system_site_packages/local/lib/python2.7/site-packages/nose/importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
  File "/home/travis/virtualenv/python2.7_with_system_site_packages/local/lib/python2.7/site-packages/nose/importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
  File "/home/travis/build/dzlab/sentimentpy/webapp/app/__init__.py", line 6, in <module>
app.config.from_object('config')
  File "/home/travis/virtualenv/python2.7_with_system_site_packages/local/lib/python2.7/site-packages/flask/config.py", line 162, in from_object
obj = import_string(obj)
  File "/home/travis/virtualenv/python2.7_with_system_site_packages/local/lib/python2.7/site-packages/werkzeug/utils.py", line 426, in import_string
sys.exc_info()[2])
  File "/home/travis/virtualenv/python2.7_with_system_site_packages/local/lib/python2.7/site-packages/werkzeug/utils.py", line 408, in import_string
return __import__(import_name)
ImportStringError: import_string() failed for 'config'. 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;

      

What's wrong with my configuration?

+3


source to share


2 answers


import_string only accepts absolute module imports. Since config

it is not a top level module but a part webapp

, you need to specify webapp.config

. See http://flask.pocoo.org/docs/0.10/config/#configuring-from-files :



app = Flask(__name__)
app.config.from_object('yourapplication.default_settings')
app.config.from_envvar('YOURAPPLICATION_SETTINGS')

      

+4


source


I recently ran into this issue and couldn't figure it out until 2 days later I got an epiphany by reading Marcus's answer.

Just in case someone out there is looking for a solution to load the config from config.py

and the flask app uses the package structure, then make sure to provide the full classpath for the config class file in app.config.from_object()

.

For example, I had a config myproj/app/config.py

in my flask project myproj

and the config class file was DevelopmentConfig

. You must provide it like this:

app.config.from_object('app.config.DevelopmentConfig')

      

Another example, if you put the same file in myproj/instance/config.py

, you will call it like this:

app.config.from_object('instance.config.DevelopmentConfig')

      



During the development of your application, the easiest way to change the settings is to put an environment variable in a file myapp/.env

, for example:

FLASK_APP=app
APP_SETTINGS="app.config.DevelopmentConfig"

      

and use a variable in your call app.config.from_object()

:

app.config.from_object(os.environ['APP_SETTINGS'])

      

But remember that in order for .env to take effect, you need to start the application with flask run

instead of starting the application directly.

0


source







All Articles