python-ImportError:Travis-CI构建上没有名为config的模块

我在Travis builds上出现导入错误,该错误与flask的配置有关:

from flask import Flask

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

在本地计算机上,flask应用程序正确运行.但是在travis上是错误跟踪

$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;

我的配置有什么问题?

解决方法:

import_string仅需要绝对模块导入.由于config不是*模块,而是webapp的一部分,因此您需要指定webapp.config.查看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')
上一篇:继承鼻子测试的基类


下一篇:使用装饰器进行重构以减少代码量