How to import files in python using sys.path.append?

You can create a path relative to a module by using a module’s __file__ attribute. For example:

myfile = open(os.path.join(
    os.path.dirname(__file__),
    MY_FILE))

This should do what you want regardless of where you start your script.

Leave a Comment