24

What is the equivalent of import * in Python using functions (presumably from importlib)?

I know that you can import a module with mod = __import__(...), which will delegate to whatever the currently configured implementation is. You can also do something like

mod_spec = importlib.utl.spec_from_file_location(...)
mod = importlib.util.module_from_spec(mod_spec)
mod_spec.loader.exec_module(mod)

which allows you to do crazy things like injecting things into the module by inserting them before the call to exec_module. (Courtesy of https://stackoverflow.com/a/67692/2988730 and https://stackoverflow.com/a/38650878/2988730)

However, my question remains. How does import * work in function form? What function determines which names to load from a module depending on the presence/contents of __all__?

Community
  • 1
  • 1
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • 3
    Afaik `from mod import *` isn't exposed in some way, there's a special byte-code generated for it and it is handled by `C` code. – Dimitris Fasarakis Hilliard Feb 01 '17 at 22:24
  • @JimFasarakis-Hilliard. While possible, I will continue to wait for an answer. Importlib exposes or at least replicates some pretty detailed functionality. I always assumed it had the entire loading mechanism available in it, but haven't been able to find this particular piece. – Mad Physicist Feb 01 '17 at 22:29
  • Removed the generic python tag. – Mad Physicist Feb 01 '17 at 22:33
  • 1
    Not sure about it either that's why I went for a comment :-). Why remove the python tag? You're excluding many people who don't lurk in the `3.x` tag from seeing it. – Dimitris Fasarakis Hilliard Feb 01 '17 at 22:34
  • 1
    @JimFasarakis-Hilliard. My question is specifically Python3 related, but I suppose you have a good point. Reinstated. – Mad Physicist Feb 01 '17 at 22:36
  • 3
    The generic `python` tag should go on all `python`-related question. If a question is *specifically* about a particular version, a more *specific* tag may be added *in addition to* the generic tag. – juanpa.arrivillaga Feb 01 '17 at 22:46

1 Answers1

22

There's no function for from whatever import *. In fact, there's no function for import whatever, either! When you do

mod = __import__(...)

the __import__ function is only responsible for part of the job. It provides you with a module object, but you have to assign that module object to a variable separately. There's no function that will import a module and assign it to a variable the way import whatever does.


In from whatever import *, there are two parts:

  • prepare the module object for whatever
  • assign variables

The "prepare the module object" part is almost identical to in import whatever, and it can be handled by the same function, __import__. There's a minor difference in that import * will load any not-yet-loaded submodules in a package's __all__ list; __import__ will handle this for you if you provide fromlist=['*']:

module = __import__('whatever', fromlist=['*'])

The part about assigning names is where the big differences occur, and again, you have to handle that yourself. It's fairly straightforward, as long as you're at global scope:

if hasattr(module, '__all__'):
    all_names = module.__all__
else:
    all_names = [name for name in dir(module) if not name.startswith('_')]

globals().update({name: getattr(module, name) for name in all_names})

Function scopes don't support assigning variables determined at runtime.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • I agree that there is no function to replace either statement exactly, but there is an equivalent function *call* (or couple of calls) that does the same thing. I am very surprised (and a little skeptical) that the selection code you have provided does not exist somewhere in importlib. – Mad Physicist Feb 01 '17 at 22:39
  • @MadPhysicist: They're not going to provide a function that assigns your variables for you. Too much magic, and it'd only ever be usable at global or class scope anyway. (Regular Python 3 `import *` is only usable at global scope, but regular `import *` inside a function can be caught at compile time). – user2357112 Feb 01 '17 at 22:44
  • That makes sense. I will wait a day or so and select your answer. – Mad Physicist Feb 01 '17 at 22:46
  • 1
    Just finally processed `fromlist=['*']`. That's the only non trivial part, so thanks for that specifically – Mad Physicist Feb 04 '17 at 20:21
  • Just to make this clear - if no `__all__` directive is present, will `from whatever import *` import _all submodules_ if whatever is a package or just the names in `whatever/__init__.py` ? – Mr_and_Mrs_D Feb 18 '17 at 09:10
  • 1
    @Mr_and_Mrs_D: It will import whatever submodules happen to be loaded at the end of `__init__.py`. Which submodules these are will depend on what other imports have been executed in the program. – user2357112 Feb 18 '17 at 16:36