[Python-ideas] Allow multiple imports from a package while preserving its namespace

Greg Ewing greg.ewing at canterbury.ac.nz
Sat Apr 28 21:28:01 EDT 2018


Nick Coghlan wrote:
> I find the imports at the top of the file to be a nice
> catalog of external dependencies.

Not only is it useful for human readers, it's also useful
for packaging tools such as py2exe that need to know which
modules are being used.

I experimented once with auto-importing in PyGUI, but in
the end I dropped it, partly because of this consideration.

There were other problems with it as well. I don't recall
all the details, but I think one issue is that any errors
resulting from an import triggered by an attribute access
get masked and turned into an AttributeError, making them
very confusing to diagnose.

Also, importing requires acquisition of the import lock,
which could cause problems in a multithreaded environment
if it happens at unpredictable times.

For these reasons I'm inclined to regard auto-importing
as an anti-pattern -- it seems like it should be a good
idea, but it leads to more problems than it solves.

-- 
Greg


More information about the Python-ideas mailing list