Renaming an import

Peter Otten __peter__ at web.de
Fri Sep 6 02:36:56 EDT 2019


Rob Gaddi wrote:

> I'm trying to figure out how to rename an import globally for an entire
> package.
>   Something like:
> 
> pkg/__init__.py:
>      import graphing_module_b as graph

If you want to go low-level:

sys.modules["pkg.graph"] = graph

will make
 
> pkg/foobar.py:
>      from .graph import plot, axis

work.
 
> The point being that, if at some point I decide to change from
> graphing_module_b to graphing_module_a, that decision is made at a single
> central point in my package rather than scattered through 30 different
> import statements in a dozen files.
> 
> Any ideas?
> 





More information about the Python-list mailing list