@pytest.yield_fixture
def tmpmod(tmpdir):
 sys.path.insert(0, str(tmpdir))
 loadedmods = set(sys.modules.keys())
 try:
  (yield tmpdir)
 finally:
  del sys.path[0]
  newmods = (set(sys.modules.keys()) - loadedmods)
  for m in newmods:
   del sys.modules[m]

@ pytest . yield_fixture def tmpmod ( tmpdir ) : sys . path . insert ( 0 , str ( tmpdir ) ) loadedmods = set ( sys . modules . keys ( ) ) try : ( yield tmpdir ) finally : del sys . path [ 0 ] newmods = ( set ( sys . modules . keys ( ) ) - loadedmods ) for m in newmods : del sys . modules [ m ]
same as tmpdir but also adds/removes it to the front of sys .