2

Does python 3.7 offer a way to import modules and alias only the submodules? Suppose I have a module named module with a submodule ridiculously_long_submodule_name that I'd like to alias with short_submodule

Is there a way to alias just the submodule so I can call it via module.short_submodule? I tried the obvious from module import ridiculously_long_submodule_name as module.short_submodule resulting in a syntax error. I'd really like to avoid importing it without the main module name in front because of confusion issues with the rest of my code

5
  • 9
    Why not just from module import ridiculously_long_submodule_name as short_submodule ? Commented Aug 30, 2018 at 7:02
  • I have other modules that use similar names, which is why I'd rather have the module in front Commented Aug 30, 2018 at 7:05
  • 2
    Not possible as far as I know. Commented Aug 30, 2018 at 7:06
  • 5
    OK, how about: from module import ridiculously_long_submodule_name as module_short_submodule? Commented Aug 30, 2018 at 7:07
  • Will have to do as a workaround I guess. I thought there would be a way to "naturally" alias the submodule. Thanks for clarifying Commented Aug 30, 2018 at 7:13

1 Answer 1

6

There's no dedicated syntax for aliasing module.ridiculously_long_submodule_name as module.short_submodule, and while you could mess with the module's attributes to do it manually, it'd be a bad idea.

Aliases set with import as are supposed to be local to the scope where the import happens, to avoid name conflicts, but setting module.short_submodule to the submodule you want would be a global change. All code in the program would see it, because unless you get really crazy with __getattribute__, there's no such thing as an object "locally" having an attribute.

Even code in module would see an unexpected new global variable. That can cause really nasty conflicts if the writers of module ever want to use the short_submodule name, but even without that happening, mucking with other libraries like this is going to confuse people who have to maintain or debug your code, including the you of 12 months from now.

Just do import module.ridiculously_long_submodule_name as submod. Don't try to put a module. prefix on the alias.

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

1 Comment

If you need some other code to see the hacked module name, you can use something like: sys.modules['sklearn.preprocessing.label'] = sys.modules['sklearn.preprocessing._label']. ... where .label is the name you want and ._label is the name that is already successfully imported. But this is a hack in the a strong jury-rigging sense of the word. Hopefully whoever is in a similar situation can get it to work just enough to move along to some better solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.