推荐学习书目
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 · Apr 24, 2015 · 3471 views
    This topic created in 4071 days ago, the information mentioned may be changed or developed.

    项目结构简略如下:
    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 replies    2015-04-24 17:44:50 +08:00
    cbsw
        1
    cbsw  
       Apr 24, 2015   ❤️ 1
    init.py 中把 import bp 这句放在最后
    vob636
        2
    vob636  
       Apr 24, 2015
    blueprint的引用一般都是放app.register_blueprint前面就好了……没人强制要求非放文件头的。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1132 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 23:11 · PVG 07:11 · LAX 16:11 · JFK 19:11
    ♥ Do have faith in what you're doing.