Packaging data files

Perhaps you want to include non-Python files inside a Python package, like in ejnug. Here's how you do it.

Opening the file in Python

Store the file that you are using in a directory inside the package that you are using it from, then get its name in Python with something like this.

loans_file = os.path.abspath(os.path.join(__file__, '..', 'fixtures', 'examples.csv'))

__file__ is the name of the file that the Python code is written in.

Packaging the files so you can install them

If you are using the file just for testing, you can ignore this section. But if you are using the file for anything else and the file thus needs to be installed with the Python package, read on.

The installer needs to know to copy data files, and you tell it so in the setup.py. Ejnug accomplishes this with the include_package_data argument, and you can also explicitly set the package_data argument.

Annoyingly, the installer doesn't give helpful error messages when you misspell these arguments or set them incorrectly, so it takes me forever to figure this out every time I want to do it.