coolrice
V2EX  ›  问与答

SQLAlchemy 的类导入问题

  •  
  •   coolrice · May 20, 2015 · 2752 views
    This topic created in 4018 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
        1
    billlee  
       May 20, 2015
    你的 __init__.py 里设置 __all__ 了吗?
    coolrice
        2
    coolrice  
    OP
       May 20, 2015
    @billlee 没有
    billlee
        3
    billlee  
       May 20, 2015
    coolrice
        4
    coolrice  
    OP
       May 21, 2015
    @billlee 貌似懂了,不过要自己手动写好,有没有方法能得到这个包下的所有模块名字?
    billlee
        5
    billlee  
       May 21, 2015
    @coolrice find models/ -maxdepth 1 -name *.py -print0 | xargs -0 basename -s .py
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5165 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 01:16 · PVG 09:16 · LAX 18:16 · JFK 21:16
    ♥ Do have faith in what you're doing.