Skip to content

Instantly share code, notes, and snippets.

@collinanderson
Created January 15, 2015 15:50
Show Gist options
  • Save collinanderson/6675dc9fdc0b84d80a49 to your computer and use it in GitHub Desktop.
Save collinanderson/6675dc9fdc0b84d80a49 to your computer and use it in GitHub Desktop.
import importlib
import os
import shutil
testmod = os.path.join(os.path.dirname(__file__), 'testmod')
os.mkdir(testmod)
with open(os.path.join(testmod, '__init__.py'), 'w') as f:
f.write('pass')
mod1 = os.path.join(testmod, 'mod1')
os.mkdir(mod1)
with open(os.path.join(mod1, '__init__.py'), 'w') as f:
f.write('pass')
importlib.import_module('testmod.mod1')
mod2 = os.path.join(testmod, 'mod2')
os.mkdir(mod2)
with open(os.path.join(mod2, '__init__.py'), 'w') as f:
f.write('pass')
try:
importlib.import_module('testmod.mod2')
finally:
shutil.rmtree(testmod)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment