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()?

6
  • 1
    from CodeModel.CodeModel import CodeModel ;-) Commented May 20, 2019 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) Commented May 20, 2019 at 20:02
  • Can you put this as an answer and I will accept? Commented May 20, 2019 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. Commented May 20, 2019 at 20:06
  • I'm not sure what you mean @mkrieger1. I want to create a class called CodeModel Commented May 20, 2019 at 20:06

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.