Q:

How to call a Python function that uses IO from another package

I have a function that loads data using the current path like this: open('./filename', 'rb').  When I call it from a module located in the same package, it works, but when I import its package from a module in a different package and call it, I get an error telling me that the path './filename' does not exist.  The error is raised by the call to open.  What is causing this, and how do I fix this? 

A:

I'm not aware of best practices, but modules have a __file__ attribute set to a string representation of the name of the file they were loaded from.  Thus, you can do this:
import os.path

# Get the directory this module is being loaded from
module_directory = os.path.dirname(__file__)

# Get the path to the file we want to open
file_path = os.path.join(module_directory, 'filename')

with open(file_path, 'rb') as f:
    # do what you want with the file

