Sometimes you will see someone importing all the functionality in a module with the following syntax:
There are many reasons why this is an awful practise and most people won't argue if you flag this issue. But why is it so bad? Well:
- Importing functionality without a clear reference makes your code basically impossible to read. This is particularly bad if there is more than one
import *
, meaning you have to check all of the wildcard imports to find the information you are looking for.
- Extra functionality can bleed in from all kinds of places. This is particularly abhorrent if one
import *
imports from another import *
making the trail of confusion even more convoluted.
- You may be missing functionality that you thought existed as part of the
*
you imported.
- It's near impossible to comprehend the state of your application's environment by simply reading the code.
- Your IDE may get confused by your wild imports meaning that you can't navigate the code easily.
- Other developers that aren't familiar with the module will get confused!
Instead you should:
- Explicitly reference the functionality that you require in the module you are writing. For example:
from my_module import specific_function, other_required_function
- Import the whole module. This way we can follow the trail of information. For example, we import the module with
import my_module
and reference the functionality within when we execute it: my_module.specific_function()
.
The only time that you miiiiight want to use import *
is when extending a config. For example:
### base_config.py
UNIVERSAL_VARIABLE: str = "abc123"
### dev_config.py
from base_config import *
DEBUG_SPECIFIC_SETTING: bool = True
But ideally config would be separate to your code...