##make sure /tmp/foobar does *not* exist run this once ##and should see 'failed' run again and see succeeded import os import sys sys.path.append("/tmp/foobar") try: import some_other_module_does_not_matter_which except: pass testcase = """ print "hi" """ try: os.mkdir("/tmp/foobar") open( "/tmp/foobar/foobar_test.py", "w" ).write( testcase ) except: print "path already exists. import should succeed" try: import foobar_test print 'succeeded' except: print 'failed'