import os import sys sys.path = sys.path[1:] # remove the curdir from path try: os.remove('impfile.py') except: pass # sys.path.append('.') # adding this here prevents the bug on pypy, but not on cpython... to prevent it on cpython it needs to be added later try: import it_does_not_exist except ImportError: pass open('impfile.py', 'w').close() import impfile # <------ this is the import that fails print('Imported impfile')