r/Python • u/RomfordNavy • Oct 08 '26
Discussion Runtime overhead of wildcard import
Assuming a large project which might have say fifty variables in Constants.py is there likely to be any runtime overhead in having from Constants import * at the beginning of each module? As far as I know this creates entries for each of those variables in the current local namespace.
I know it could be done as import constants as x which after the first import then has virtually no overhead, but that then means referring to all constants as x.const1...
18
Upvotes
View all comments
2
u/VanillaOk4593 Oct 09 '26
from constants import *doesn't copy the constants. It adds one reference per name to the importing module's globals dict, pointing at objects that already live in the cached module. That happens once, at import time. After that, a global lookup is a dict lookup and takes the same time with 10 names or 500.So yes, the unused names sit in the namespace, unlike a compiled include, but each one costs a few bytes. The alternative is
import constants as c, and there everyc.FOOadds an attribute lookup, so the star import is actually slightly faster at access. If you keep it,__all__in constants.py controls which names get pulled in.