Improvement to imports, what is a better way ?

Mats Wichmann mats at wichmann.us
Wed Jan 18 14:56:39 EST 2023


On 1/18/23 12:29, Paul Bryan wrote:
> I would suggest allowing each module to define its own imports, don't
> import what a module doesn't consume, keep them simple, avoid devising
> a common namespace for each, and let tools like isort/black work out
> how to order/express them in source files.

Indeed.  And various checkers will report on unused imports.

These add no value... why import something "as" and give it its own name?

>> import os as                        os
>> import sys as                       sys
>> import importlib as                 importlib

A general comment: there are some very common "import ... as" idioms 
(for example, it seems like *everyone* does "import pandas as pd") and 
those are okay to follow, but in general I would stay away from trying 
to give everything short-names.  Each module imported with a name other 
than their own is a memory burden for the reader (maybe even for you!).

>> import aboutTime as         tt      # Time dates timestamps and the
>> like
>> import avMedia as           av      # Audio and maybe video 'someday'
>> well definitely lots of TTS text to speech
>> import basicSwitchboard as  sc      # First switchboard lurking.
>> Kickoff to sequence viewers

Any decent editor these days will autocomplete for you, so there's 
really not much if any typing burden in using the full names.



More information about the Python-list mailing list