开发者

Python Submodules an easier reference

开发者 https://www.devze.com 2023-02-23 06:38 出处:网络
I don\'t know if this is possible in python but here goes ... I have a folder structure similar to this

I don't know if this is possible in python but here goes ...

I have a folder structure similar to this

\webapplication
    \forms
        __init__.py
        model01.py  # (defines Model01Form)
        model02.py  # (defines Model02Form)
    \views
        __init__.py
        admin.py    # (this file imports the forms)

I was wondering if there is any way to import like this but still keep them in separate files.

开发者_开发百科
from webapplication.forms import Model01Form, Model02Form

Instead of having to do this

from webapplication.forms.model01.Model01Form
from webapplication.forms.model02.Model02Form

thanks in advance :)


Only if you import the forms into __init__.py in the first place:

from model01 import Model01Form
from model02 import Model02Form

However, you should ask yourself if you necessarily need to have two separate model files. Python is not Java: you can have as many classes in a module as you like.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号