开发者

Python imports-Need someone to check this please

开发者 https://www.devze.com 2023-01-20 19:14 出处:网络
Ok yes it is a very silly question, but just that I am getting a little confused. I have a file structure which looks like this:-

Ok yes it is a very silly question, but just that I am getting a little confused. I have a file structure which looks like this:-

-Mainapplication
   -models.py
-Helpingmodules
   -Folder1
     -module1.py

Now I have to import models into module1. So in module1.py I just did:-

from Mainapplication import models 

Now this does work fine, but I get a feeling that it might be wr开发者_运维技巧ong. Can someone please let me know if this is the correct way.


There's nothing wrong with the import, but if the names of your packages are accurate, this looks like a design flaw in that you're destroying code reusability; I'd expect a package of "helping modules" to be independent of the application they're helping (although in the case the package name is so vague that I could be way off about their purpose.)


There's nothing wrong with your import.

You could say:

import Mainapplication.models

but then you'd have to reference models with its Package prefix every time you used it, e.g.:

Mainapplication.models.foo("bar")

The way you've done it allows you to use the following form which is usually preferable:

models.foo("bar")

For the full story you can read the documentation.

0

精彩评论

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