0

I'd like to divide some abstract code from its implementation and link the actual implementation as import in __init__.py.

My 'model/__init__.py' looks like this:

from .estimator import AbstractEstimator
# link default estimator implementation
from impl.tf import TF_Estimator as Estimator

However, TF_Estimator is a sub-class of 'model.AbstractEstimator':

from model import AbstractEstimator
class TF_Estimator(AbstractEstimator):
    ...

How can I resolve this cyclic dependency? Is there some way to "ignore" the import of 'TF_Estimator' in 'model/__init__.py'?

My target would be that I can run "from model import Estimator" and get "TF_Estimator" back in the end.

Hoeze
  • 636
  • 5
  • 20
  • 1
    I believe I asked myself a similar question a while ago. Mine was about exceptions, but the core problem was to structure a package to avoid cyclic import. Can that help? https://stackoverflow.com/questions/48104895/best-practice-for-structuring-module-exceptions-in-python3 – Olivier Melançon Mar 09 '18 at 18:37
  • 2
    `model.AbstractEstimator` is just another name for `.estimator.AbstractEstimator`. Why can't you use that instead? The `model` namespace should be considered the public interface; it doesn't necessarily have to be used by your implementation. – chepner Mar 09 '18 at 18:39
  • @chepner: OMG, i did not see that obvious solution... Thanks! – Hoeze Mar 09 '18 at 19:06
  • @Olivier: Thank you for your pointer. I'll keep that in mind as a more general solution. – Hoeze Mar 09 '18 at 19:06

1 Answers1

-1

The solution was pretty simple, thanks to @chepner: Simply use from model.estimator import AbstractEstimator in TF_Estimator; This skips loading 'model/__init__.py'

Hoeze
  • 636
  • 5
  • 20