Skip to content

Instantly share code, notes, and snippets.

@nsmaciej
Last active August 29, 2015 14:18
Show Gist options
  • Save nsmaciej/a972dbf943078256372f to your computer and use it in GitHub Desktop.
Save nsmaciej/a972dbf943078256372f to your computer and use it in GitHub Desktop.
Python module shadowing
# Call this file "datetime.py" (or any other stdlib name)
# and import it. It will behave just as if it was the
# library itself, but contain a 'fake' attribute
# $ ls
# datetime.py
# $ python
# >>> import datetime
# >>> datetime.fake
# True
# >>>
import sys
__builtins__['n'] = __name__
sys.path.remove('')
sys.modules.pop(n)
obj = __import__(n)
vars(obj)['fake'] = True
__builtins__.pop('n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment