Package data
A package might contain more than just Python source files. Sometimes, you might need to include other types of files as well. For example, a package may include one or more image files, a large text file containing a list of all the ZIP codes in the USA, or any other type of data you may need. If you can store something in a file, you can include this file as part of your Python package.
Normally, you would place your package data in a separate sub-directory within the package directory. To access these files, your package needs to know where to find this sub-directory. While you could hardwire the location of this directory into your package, this won't work if your package is to be reused or moved. It's also not necessary as you can easily find the directory in which a module resides by using the following code:
cur_dir = os.path.abspath(os.path.dirname(__file__))
This gives you the complete path to the directory containing the current module. Using the os.path.join()
function...