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

Robert Vanden Eynde robertve92 at gmail.com
Thu Apr 26 12:54:42 EDT 2018


I just ran into a similar problem, how to relatively import without binding
the submodule.

Let's say you have this :
myapp/
    urls.py
    views/
        base.py

When you're in urls.py and you want to relatively access Functions from
base.py, you must use the from syntax.

from .views import base
base.func()

But what if I just want "views" in my namespace?

from . import views
from .views import base
views.base.func()
base.func()

import myapp.views.base
myapp.views.base.func()

from . import views
import myapp.views.base
views.base.func()
myapp.views.base.func()

Le jeu. 26 avr. 2018 à 17:24, Chris Angelico <rosuav at gmail.com> a écrit :

> On Thu, Apr 26, 2018 at 11:53 PM, Julian DeMille via Python-ideas
> <python-ideas at python.org> wrote:
> > That's the kind of thing I'm looking for. I've dealt with some library
> > authors who were highly against importing the root allowing me to access
> > submodules with hierarchy.
>
> With a package, having automatic imports forces those submodules to be
> loaded eagerly (as soon as you import the package, you load up those
> modules). Lazily-loaded submodules can improve performance if you
> don't always need them.
>
> +0 for an easier way to import multiple submodules at once. It's not
> something I've personally had a need for, but it's a sane and logical
> thing to do.
>
> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180426/b04bbbbc/attachment.html>


More information about the Python-ideas mailing list