Skip to content

Instantly share code, notes, and snippets.

@alorence
Created September 24, 2024 19:04
Show Gist options
  • Save alorence/5bfe71d51d4c90578cedef7ccf70fb33 to your computer and use it in GitHub Desktop.
Save alorence/5bfe71d51d4c90578cedef7ccf70fb33 to your computer and use it in GitHub Desktop.
import importlib
def import_all_from(module_path: str) -> None:
"""
This is a programmatic version of "from <module_path> import *"
It can be used to import all variables from specific settings file and inject it in current globals()
:param module_path: A dotted path to a python module
"""
module = importlib.import_module(module_path)
globals().update({key: value for key, value in module.__dict__.items() if not key.startswith("_")})
import_all_from("project.settings.main")
import_all_from("project.settings.foo")
import_all_from("project.settings.bar")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment