0

My older structure is:

enter image description here

In a file, I'm doing:

from CodeModel import CodeModel

codemodel = CodeModel.CodeModel()

But that seems redundant. Is there a cleaner way to import CodeModel without having to do CodeModel.CodeModel()?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Shamoon
  • 41,293
  • 91
  • 306
  • 570
  • 1
    `from CodeModel.CodeModel import CodeModel` ;-) – thebjorn May 20 '19 at 19:56
  • 1
    Or put `from .CodeModel import CodeModel` in your `__init__.py`. Then `from CodeModel import CodeModel` from outside the package will import the class. (You should probably give the packages, modules, and classes different names though, or it quickly becomes very confusing) – Patrick Haugh May 20 '19 at 20:02
  • Can you put this as an answer and I will accept? – Shamoon May 20 '19 at 20:02
  • Why is `CodeModel.py` even in a package called `CodeModel`? There seems to be nothing else in it. Just remove that package. – mkrieger1 May 20 '19 at 20:06
  • I'm not sure what you mean @mkrieger1. I want to create a class called `CodeModel` – Shamoon May 20 '19 at 20:06
  • Possible duplicate of [Importing class from another file](https://stackoverflow.com/questions/41276067/importing-class-from-another-file) – mkrieger1 May 20 '19 at 20:07

1 Answers1

2
from CodeModel.CodeModel import CodeModel

but you should think of different packages&modules structure because it may indeed be redundant.

If you have multiple (but not really many) models, think of creating modules.py with CodeModel and other model classes. Simplify things if possible (adequatly to project's size).

from models import CodeModel

seems better, doesn't it?

Another option would be

from .CodeModel import CodeModel

inside __init__.py of CodeModel package, already mentioned in the comment by Patrick Haugh.

asikorski
  • 882
  • 6
  • 20