scrapy 怎么设置多个环境配置文件

在scrapy框架里面,setting.py是整个项目的配置文件

我本地开发环境的mysql配置信息如果写在了setting.py里面,那么线上的环境的setting.py也会存放一模一样的mysql配置信息

有没有办法,可以区分本地和线上环境,从而读取不同的配置文件。比如本地环境就读取‘local_setting.py’,生产环境就读取‘sever_setting.py’?

阅读 7.9k
4 个回答

在scrapy项目中有个scrapy.cfg文件,里面有一项指定了settings文件

[settings]
default = Spider.settings

有同样问题,楼上两个答案并未解决该问题。

最后采用了scrapyd-client的README提供的在配置文件中import一个不会被部署的配置文件,通过覆盖配置来实现环境区分的解决方案,不够优雅,但足够实用:

Local Settings

You may want to keep certain settings local and not have them deployed to Scrapyd. To accomplish this you can create a local_settings.py file at the root of your project, where your scrapy.cfg file resides, and add the following to your project's settings:

try:
    from local_settings import *
except ImportError:
    pass

scrapyd-deploy doesn't deploy anything outside of the project module, so the local_settings.py file won't be deployed.

你好,你的问题解决了吗?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题