[Python-ideas] Implicit submodule imports

M.-A. Lemburg mal at egenix.com
Wed Sep 24 20:10:27 CEST 2014


On 24.09.2014 19:57, Thomas Gläßle wrote:
> Hey folks,
> 
> What do you think about making it easier to use packages by
> automatically importing submodules on attribute access.
> 
> Consider this example:
> 
>     >>> import matplotlib
>     >>> figure = matplotlib.figure.Figure()
>     AttributeError: 'module' object has no attribute 'figure'
> 
> For the newcomer (like me some months ago) it's not obvious that the
> solution is to import matplotlib.figure.
> 
> Worse even: it may sometimes/later on work, if the submodule has been
> imported from another place.
> 
> How, I'd like it to behave instead (in pseudo code, since `package` is
> not a python class right now):
> 
>     class package:
> 
>         def __getattr__(self, name):
>             try:
>                 return self.__dict__[name]
>             except KeyError:
>                 # either try to import `name` or raise a nicer error message
> 
> The automatic import feature could also play nicely when porting a
> package with submodules to or from a simple module with namespaces (as
> suggested in [1]), making this transition seemless to any user.
> 
> I'm not sure about potential problems from auto-importing. I currently
> see the following issues:
> 
> - harmless looking attribute access can lead to significant code
> execution including side effects. On the other hand, that could always
> be the case.
> 
> - you can't use attribute access anymore to test whether a submodule is
> imported (must use sys.modules instead, I guess)
> 
> 
> In principle one can already make this feature happen today, by
> replacing the object in sys.modules - which is kind of ugly and has
> probably more flaws. This would also be made easier if there were a
> module.__getattr__ ([2]) or "metaclass" like feature for modules (which
> would be just a class then, I guess).
> 
> Sorry, if this has come up before and I missed it. Anyhow, just
> interested if anyone else considers this a nice feature.

Agreed, it's a nice feature :-)

I've been using this in our mx packages since 1999 using a module
called LazyModule.py. See e.g.
http://educommons.com/dev/browser/3.2/installers/windows/src/eduCommons/python/Lib/site-packages/mx/URL/LazyModule.py

Regarding making module more class like: we've played with this
a bit at PyCon UK and it's really easy to turn a module into a
regular class (with all its features) by tweaking sys.modules -
we even got .__getattr__() to work. With some more effort, we
could have a main() function automatically called upon direct
import from the command line.

The whole thing is a huge hack, though, so I'll leave out the
details :-)

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Sep 24 2014)
>>> Python Projects, Consulting and Support ...   http://www.egenix.com/
>>> mxODBC.Zope/Plone.Database.Adapter ...       http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________
2014-09-27: PyDDF Sprint 2014 ...                           3 days to go
2014-09-30: Python Meeting Duesseldorf ...                  6 days to go

::::: Try our mxODBC.Connect Python Database Interface for free ! ::::::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/


More information about the Python-ideas mailing list