Skip to content

Instantly share code, notes, and snippets.

@hongqn
Last active August 4, 2020 16:25
Show Gist options
  • Save hongqn/9a41725180231bfae7380932b9b4c367 to your computer and use it in GitHub Desktop.
Save hongqn/9a41725180231bfae7380932b9b4c367 to your computer and use it in GitHub Desktop.

$ cat ./productB.py

import os

os.environ["PRODUCT"] = "B"

import feature

print(feature.COST)

$ cat ./productA.py

import os

os.environ["PRODUCT"] = "A"

import feature

print(feature.COST)

$ cat ./feature/A.py

COST = 1

$ cat ./feature/B.py

COST = 2

$ cat ./feature/__init__.py

import os
import importlib

_product = os.environ["PRODUCT"]

_mod = importlib.import_module(f".{_product}", __name__)
globals().update((k, v) for k, v in _mod.__dict__.items() if not k.startswith("_"))
$ python productA.py
1

$ python productB.py
2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment