import importlib import os import sys import zipfile # Create a zipfile with a module zf = zipfile.ZipFile("modules.zip", "w") zf.writestr("hello/__init__.py", "'hello'\n") zf.close() # Prepend to sys.path sys.path.insert(0, "modules.zip") # Import the module to initialize # zipimport import hello # Now remove the zipfile without removing # it from sys.path os.unlink("modules.zip") # Invalidate importlib caches (because we # change sys.path) importlib.invalidate_caches() # Try to import a random module import uu