coolrice
V2EX  ›  问与答

SQLAlchemy 的类导入问题

  •  
  •   coolrice · May 20, 2015 · 3087 views
    This topic created in 4186 days ago, the information mentioned may be changed or developed.

    我本来是吧所有类放到一个models.py文件下,每个类继承相同的Base,这样在建表的时候直接import models模块
    现在我想把类分开放到不同的文件里,但都放到models包里,但是这样我在from models import * 时就是不能创建表,只能from models import user 这样才行,又没有什么好的方法直接导入全部呢?

    5 replies  •  2015-05-21 13:23:28 +08:00
    billlee
    billlee
    May 20, 2015
    1
    你的 __init__.py 里设置 __all__ 了吗?
    coolrice
    coolrice
    OP
    May 20, 2015
    2
    @billlee 没有
    coolrice
    coolrice
    OP
    May 21, 2015
    4
    @billlee 貌似懂了,不过要自己手动写好,有没有方法能得到这个包下的所有模块名字?
    billlee
    billlee
    May 21, 2015
    5
    @coolrice find models/ -maxdepth 1 -name *.py -print0 | xargs -0 basename -s .py
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1405 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 22:49 · PVG 06:49 · LAX 14:49 · JFK 17:49
    ♥ Do have faith in what you're doing.