1

I am trying to restructure an old python package that has some code inside its __init__.py and I want to:

  • Raise a deprecation warning when someone does from mypackage import Foo
  • Avoid a deprecation warning when someone does python -m mypackage

That package can be executed via its __main__.py, but by the time __main__ file is opened, python already loaded the __init__ file.

Is there a way to detect this and add a conditional into __init__.py?

The goal is to deprecate all historical code form init and move it in modules, but I am trying to avoid breaking consumers and give them time to migrate their imports.

I should emphasized textmention that minimal version of python supported is py36.

wim
  • 302,178
  • 90
  • 548
  • 690
sorin
  • 149,293
  • 163
  • 498
  • 754
  • [Module `__getattr__`](https://stackoverflow.com/q/2447353/674039) is the way to do this in Python 3.7+. In Python 3.6 you only have dirty hacks avail. – wim Jun 03 '20 at 19:20
  • https://stackoverflow.com/a/47854417/3679490 basically you override the import module – vks Jun 03 '20 at 19:29

0 Answers0