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

[求助]flask celery 配置结构问题

  •  
  •   tomnee · Mar 22, 2014 · 5774 views
    This topic created in 4570 days ago, the information mentioned may be changed or developed.
    脚手架是这样设置的

    --project
    -- auth
    -- main
    -- templates
    __init__.py
    factory.py(create_app, make_celery)
    utils.py
    tasks.py
    ...

    在tasks.py 里 from .factory import celery 或者 make_celery 函数 都会得出 无法import 的错误
    6 replies  •  1970-01-01 08:00:00 +08:00
    messense
        1
    messense  
       Mar 22, 2014
    是不是在 project/ 路径下直接执行 tasks.py 报错?
    tomnee
        2
    tomnee  
    OP
       Mar 22, 2014
    @messense 启动测试服务器报错 因为有模块引用了tasks.py里的测试方法
    messense
        3
    messense  
       Mar 22, 2014   ❤️ 1
    @tomnee 我以前也遇到过类似错误,一般都是因为把整个 project/ 当成一个 Python package 然后又在 project/ 路径下执行程序(启动测试服务器等等)导致错误。理论上无法 import 的错误都是因为路径不在 sys.path 里导致的,可是试试在你的 app.py(假设)里用

    try:
    ----import project
    except ImportError:
    ----import site
    ----site_dir = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
    ----site.addsitedir(site_dir)

    把当前 project 加入到 site-package 路径中去。

    更好地解决方案是别把 project/ 当成 package,而是在里面建一个同名的 package,参考 june 的代码结构。

    https://github.com/pythoncn/june
    mailto1587
        4
    mailto1587  
       Mar 22, 2014   ❤️ 1
    tomnee
        5
    tomnee  
    OP
       Mar 22, 2014
    @mailto1587
    这篇文章我看过,我的结构也借鉴了他的.但就是从 .factory import make_celery 出了问题

    project/
    project/
    factory.py
    task.py

    from .factory import make_celery

    启动测试服务器就会导入出错
    oio
        6
    oio  
       Mar 25, 2014 via iPad
    circular import, refractor your program.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2382 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 05:45 · PVG 13:45 · LAX 22:45 · JFK 01:45
    ♥ Do have faith in what you're doing.