0

I currently have the following package structure

mypkg/
|-__init__.py
|-foo/
|--_init__.py
|--foo_one.py
|--foo_two.py
|--foo_three.py
|-bar/
|-__init__.py
|- ...

Inside foo_one.py, foo_two.py and foo_three.py I defined three classes, FooOne, FooTwo and FooThree.

I would like to be able to import them as

from mypkg.foo import FooOne, FooTwo

At the moment the solution I'm using is the following:

# mypkg/foo/__init__.py

from .foo_one import FooOne
from .foo_two import FooTwo
from .foo_three import FooThree

But I think that what this actually does is importing all the classes, regardless of the specific import statement I use.

I would like to avoid that since the three classes require different additional import statements.

Also I would like to keep the three classes in three different files.

What do you suggest?

  • I think the real issue is `the three classes require different additional import statements`. Try to encapsulate required additions into the same package. – Muposat Oct 11 '17 at 06:59
  • Could you please elaborate? Each of my classes requires different external libraries. I would like to avoid importing all of them if it is not needed. I think the question is pretty general, but specifically in my case I am developing interfaces for laboratory instruments. So I want them to belong to a common `interface` module, which has a class for each interface. Still each instrument require different external libraries. – Matteo Pompili Oct 11 '17 at 08:35
  • Files foo_one.py, foo_two.py and foo_three.py should have "external libraries" contained inside. To use these libraries one should not need to import anything additional. Look how the standard libs are defined: you never need to import anything else to use them. If methods/functions return object defined elsewhere, the client will be able to use these object without importing their definitions. – Muposat Oct 11 '17 at 12:57
  • 1
    Of course my opinion is mostly speculation based on vague description of the problem. You might need to show us an example of the issue. – Muposat Oct 11 '17 at 12:59

0 Answers0