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

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

  •  
  •   tomnee · 2014-03-22 10:54:34 +08:00 · 4396 次点击
    这是一个创建于 3688 天前的主题,其中的信息可能已经有所发展或是发生改变。
    脚手架是这样设置的

    --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 条回复    1970-01-01 08:00:00 +08:00
    messense
        1
    messense  
       2014-03-22 12:26:14 +08:00
    是不是在 project/ 路径下直接执行 tasks.py 报错?
    tomnee
        2
    tomnee  
    OP
       2014-03-22 12:52:17 +08:00
    @messense 启动测试服务器报错 因为有模块引用了tasks.py里的测试方法
    messense
        3
    messense  
       2014-03-22 13:04:11 +08:00   ❤️ 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  
       2014-03-22 14:05:49 +08:00   ❤️ 1
    tomnee
        5
    tomnee  
    OP
       2014-03-22 14:38:35 +08:00
    @mailto1587
    这篇文章我看过,我的结构也借鉴了他的.但就是从 .factory import make_celery 出了问题

    project/
    project/
    factory.py
    task.py

    from .factory import make_celery

    启动测试服务器就会导入出错
    oio
        6
    oio  
       2014-03-25 23:19:12 +08:00 via iPad
    circular import, refractor your program.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2867 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 11:30 · PVG 19:30 · LAX 04:30 · JFK 07:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.