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
shunia
V2EX  ›  Python

初学 python 后端,用的是 flask,有个错误不懂

  •  
  •   shunia · 2015-04-24 15:42:41 +08:00 · 2940 次点击
    这是一个创建于 3289 天前的主题,其中的信息可能已经有所发展或是发生改变。

    项目结构简略如下:
    python
    app/
    __init__.py
    myclass.py
    bp/
    mybp.py

    _init_.py:

    from .bp.mybp import bp
    ......
    something= Something(app)
    ......
    app.register_blueprint(bp)

    myclass.py:

    from app import something
    ......
    my_class_attr = None

    mybp.py:

    from ..myclass import my_class_attr

    简单描述就是app里import了mybp,mybp里import了myclass,myclass里又import了app.运行时会报myclass.py里的错误:cannot import name something.另外不管那个from ... import ...改成什么形式基本都会有错误.
    我google了一下可能是循环引用(?)的问题,但是so到的答案并不能解决我的问题.所以:

    • 我想请各位帮忙确认一下,这是循环引用错误吗?或者是一个什么类型的错误?
    • 比较好的解决目前这个问题的方案是什么?

    提前感谢,渣水准描述可能有疑问,我随时解答.

    2 条回复    2015-04-24 17:44:50 +08:00
    cbsw
        1
    cbsw  
       2015-04-24 15:46:45 +08:00   ❤️ 1
    init.py 中把 import bp 这句放在最后
    vob636
        2
    vob636  
       2015-04-24 17:44:50 +08:00
    blueprint的引用一般都是放app.register_blueprint前面就好了……没人强制要求非放文件头的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1759 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 16:40 · PVG 00:40 · LAX 09:40 · JFK 12:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.