Instead of __file__ use os.getcwd() to get the directory where the file is located. Sphinx-Gallery executes the examples scripts in their source directory.