推荐学习书目
› 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
PanameraM
V2EX  ›  Python

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

  •  
  •   PanameraM · Sep 12, 2017 · 2968 views
    This topic created in 3302 days ago, the information mentioned may be changed or developed.

    工程结构是这样的

    |-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 replies  •  2017-09-19 19:10:28 +08:00
    wellsc
        1
    wellsc  
       Sep 13, 2017
    arischow
        2
    arischow  
       Sep 13, 2017 via iPhone
    提示:看 flask 文档。
    lbfeng
        3
    lbfeng  
       Sep 13, 2017
    app.config.from_object(config)

    app.config['xxxx']
    Rob007
        4
    Rob007  
       Sep 13, 2017
    current_app.config['xx']
    zhusimaji
        5
    zhusimaji  
       Sep 19, 2017 via iPhone
    使用 flask 程序上下文解决
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5395 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 02:29 · PVG 10:29 · LAX 19:29 · JFK 22:29
    ♥ Do have faith in what you're doing.