How to shorten similar Python import statements?

Question:

I’ve a large custom project package for Digital-Assistant program in Python.

In order to use several files in other sub-packages of same lib, I’ve to import many files having same sub-path in the import statement, which makes it look redundant.

For example :

from mylibs.master_lib.digital_assistant.web_bots.google_bot import searchGoogle
from mylibs.AI.CV import *
from mylibs.master_lib.digital_assistant.web_bots.social_media_connect import wa_pag
from mylibs.master_lib.digital_assistant.languages import access_dictionary
from mylibs.master_lib.digital_assistant.digital_assistant import speech_to_text
from mylibs.master_lib.digital_assistant.user_interface import *
from mylibs.master_lib.digital_assistant.web_bots.social_media_connect import *
from mylibs.master_lib.data_science.data_analytics import *
from mylibs.master_lib.digital_assistant.webAppsOC import *
from mylibs.master_lib.digital_assistant.device_controller import *
from mylibs.master_lib.digital_assistant.computer_graphics import *
from mylibs.master_lib.digital_assistant.desktopAppsFiles import *
from mylibs.AI.NLP import *

In the above snippet, the statement ‘mylibs.master_lib.digital_assistant‘ is common to many imports. (similar to huge libraries like Django, Tensorflow, etc)

I guess giving short names to libs isn’t an option, as its obvious all heavy packages prefer detailed nomenclatures.

Is there any way to give an alternative small name to those big identical import sub-paths ?
Asked By: HIMANSHU PANDEY

||

Answers:

These situations occur in case of huge projects like Semi-Detached and Embedded types of projects according to COCOMO Model

Method 1

Make use of exec function, f-strings and for loop to achieve this

This method is useful in case of large amount of imports which has no desire to maintain readability

for module in ["web_bots.google_bot import searchgoogle", "web_bots.social_media_connect import wa_pag", "languages import access_dictionary", "digital_assistant import speech_to_text","user_interface import *", "web_bots.social_media_connect import *", "webappsopenclose import *"]:
    exec(f"from mylibs.master_lib.digital_assistant.{module}")

for module in [".data_science.data_analytics import *", ".device_controller import *", ".artificial_intelligence.cv.computer_graphics import *"]:
    exec(f"from mylibs.master_lib.{module}")

Method 2

In case you want to maintain the readability of program
Make use of exec function along with f-strings to alter length of big imports

MM, DA = "from mylibs.master_lib", "digital_assistant"
exec(f"{MM}.{DA}.web_bots.google_bot import searchgoogle")
exec(f"{MM}.{DA}.web_bots.social_media_connect import wa_pag")
exec(f"{MM}.{DA}.languages import access_dictionary")
exec(f"{MM}.{DA}.digital_assistant import speech_to_text")
exec(f"{MM}.{DA}.user_interface import *")
exec(f"{MM}.{DA}.web_bots.social_media_connect import *")
exec(f"{MM}.{DA}.webappsopenclose import *")
exec(f"{MM}.data_science.data_analytics import *")
exec(f"{MM}.device_controller import *")
exec(f"{MM}.artificial_intelligence.cv.computer_graphics import *")

Method 3

You can also use importlib

import importlib
importlib.import_module(".Add_Ons", "MyLibs.GrandCollection.DS.Strings")

But this method is feasible in importing modules instead of functions or constants directly.

Answered By: HIMANSHU PANDEY
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.