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

时间:2014-09-20 19:03:11

标签: python configuration flask nose travis-ci

我在Travis builds上遇到导入错误,错误与烧瓶的配置有关:

from flask import Flask

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

在本地计算机上,烧瓶应用程序正确运行。但是在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;

我的配置有什么问题?

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:0)

我最近遇到了这个问题,直到第二天我通过阅读马库斯的回答顿悟后才知道。

万一有人在外面寻找从config.py加载配置的解决方案,并且flask应用正在使用包结构,然后确保提供完整的 classpath app.config.from_object()中的配置类文件。

例如,我在烧瓶项目myproj/app/config.py中有一个配置myproj,而具有该配置的类文件是DevelopmentConfig。您需要提供以下信息:

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

另一个示例是,如果将相同的文件放在myproj/instance/config.py下,则称它为:

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

在开发应用程序期间,更改设置的最简单方法是将环境变量放在myapp/.env文件中,如下所示:

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

并在您的app.config.from_object()调用中使用变量:

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

但是请记住,要使.env生效,您需要使用flask run启动应用,而不是直接运行该应用。