0

I am doing the fat models approach, so I have transformed my models.py into a package thus:

+--polls/
|  +--models/
|    +--__init__.py
|    +--__shared_imports.py
|    +--Choice.py
|    +--Question.py

My main part of the question is the __shared_imports.py: I realized that we've common import statements in various modules in the package and decided to to have that file to do the imports, then in my modules I write this:

from __shared_imports.py import *

Everything works fine, but just want to know if this approach is good. I'll appreciate your thoughts on this.

ProfNandaa
  • 3,210
  • 2
  • 18
  • 19
  • Forget about the naming of my `__shared_imports.py`, I can have that changed, my main question is around the **approach**. Thanks @volodymr for raising that. – ProfNandaa Nov 28 '15 at 02:36
  • What exactly is in the `__shared_imports.py`? Is it just importing things from elsewhere? – shuttle87 Nov 28 '15 at 02:36
  • Yes, other than having to redo the same import statements in my modules, since in normal `models.py` we will have all the imports in one place. Does that make sense? @shuttle87 Then I leave the rest of specific imports that are not shared for the respective modules in the package. – ProfNandaa Nov 28 '15 at 02:39

2 Answers2

2
  1. Use only lower case when you name modules.
  2. Do not use double underscore for module name.
  3. Read PEP8 and Google Python style guide.
  4. Use less verbose names for modules. For example: shared_imports.py -> shared.py

Got it.

In this case you need to import everything in __init__.py. Then you can export all names as __all__ = ['Choice', 'Question']

So, it will be enough just to import models package.

Example: __ init __.py

import Choice
import Question

__all__ = ['Choice', 'Question']
Pang
  • 9,564
  • 146
  • 81
  • 122
1

avoid import * because it will prevent tools like pyflakes from determining undefined variables.

to move all of it in a subdirectory and splitting it into separate files is not a bad idea, albeit mostly not needed. when your models.py file gets big, you should rather be thinking about splitting the project up into smaller apps.

cleder
  • 1,637
  • 14
  • 15