Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the function form of star import in Python 3

Tags:

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__?

like image 867
Mad Physicist Avatar asked Feb 01 '17 22:02

Mad Physicist


People also ask

What is import * in Python?

Python code in one module gains access to the code in another module by the process of importing it. The import statement is the most common way of invoking the import machinery, but it is not the only way. Functions such as importlib.

What is the use of * asterisk with import in Python?

It imports everything (that is not a private variable, i.e.: variables whose names start with _ or __ ), and you should try not to use it according to "Properly importing modules in Python" to avoid polluting the local namespace.

Should we import * in Python?

Using import * in python programs is considered a bad habit because this way you are polluting your namespace, the import * statement imports all the functions and classes into your own namespace, which may clash with the functions you define or functions of other libraries that you import.

What does __ import __ do in Python?

The __import__() in python module helps in getting the code present in another module by either importing the function or code or file using the import in python method. The import in python returns the object or module that we specified while using the import module.


1 Answers

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.

like image 142
user2357112 supports Monica Avatar answered Sep 22 '22 17:09

user2357112 supports Monica