半夜,问个 flask import 模块的问题。

2017-09-12 23:49:03 +08:00
 Tianny

工程结构是这样的

|-flasky

  |-app/  # 可以扩展复制的 app,根据需求
    |-templates/
    |-static/
    |-main/
      |-__init__.py
      |-errors.py
      |-forms.py
      |-views.py
    |-__init__.py
    |-email.py
    |-models.py

  |-migrations/  # 作为迁移使用

  |-tests/  # 测试用例
    |-__init__.py
    |-test*.py

  |-venv/  # virtualenv 的使用环境

  |-requirements.txt  # pip install -r requirements.txt
  |-config.py  # 配置文件
  |-manage.py  # 启动文件,入口

我的需求是,想在 app/main/views.py 中导入 config.py 中的内容,config.py 不在 app 这个包中。

可以在 app/main/views.py 中这样写么?

import sys
sys.path.append('...')
import config

希望大家帮帮忙,给个解决办法。

2300 次点击
所在节点    Python
5 条回复
wellsc
2017-09-13 00:00:34 +08:00
arischow
2017-09-13 00:05:12 +08:00
提示:看 flask 文档。
lbfeng
2017-09-13 00:28:46 +08:00
app.config.from_object(config)

app.config['xxxx']
Rob007
2017-09-13 13:23:01 +08:00
current_app.config['xx']
zhusimaji
2017-09-19 19:10:28 +08:00
使用 flask 程序上下文解决

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/390189

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX