V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Tianny
V2EX  ›  Python

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

  •  
  •   Tianny · 2017-09-12 23:49:03 +08:00 · 2297 次点击
    这是一个创建于 2411 天前的主题,其中的信息可能已经有所发展或是发生改变。

    工程结构是这样的

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

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

    5 条回复    2017-09-19 19:10:28 +08:00
    wellsc
        1
    wellsc  
       2017-09-13 00:00:34 +08:00
    arischow
        2
    arischow  
       2017-09-13 00:05:12 +08:00 via iPhone
    提示:看 flask 文档。
    lbfeng
        3
    lbfeng  
       2017-09-13 00:28:46 +08:00
    app.config.from_object(config)

    app.config['xxxx']
    Rob007
        4
    Rob007  
       2017-09-13 13:23:01 +08:00
    current_app.config['xx']
    zhusimaji
        5
    zhusimaji  
       2017-09-19 19:10:28 +08:00 via iPhone
    使用 flask 程序上下文解决
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2862 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 13:17 · PVG 21:17 · LAX 06:17 · JFK 09:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.