Package data

In most cases you probably won't have to include the package data, but in the cases where you do need data to go with your package, there are a few different options. First, it is important to know which files are included in your package by default:

  • Python source files in the package directories recursively
  • The setup.py and setup.cfg files
  • Tests: test/test*.py
  • All *.txt and *.py files in the examples directory
  • All *.txt files in the root directory

So after the defaults, we have the first solution: the package_data argument to the setup function. The syntax for that is simple enough, a dictionary where the keys are the packages and the values are the patterns to include:

package_data = {
    'docs': ['*.rst'],
}

The second solution is using a MANIFEST.in file. This file contains patterns to include, exclude, and more. The include and exclude commands use patterns to match. These patterns are glob-style patterns (see the glob module for documentation: https://docs.python.org/3/library/glob.html) and have three variants for both the include and exclude commands:

  • include/exclude: These commands only work for the given path and nothing else
  • recursive-include/recursive-exclude: These commands are similar to the include/exclude commands but process the given paths recursively
  • global-include/global-exclude: Be very careful with these, they will include or exclude these files anywhere within the source tree

Besides the include/exclude commands, there are also two others; the graft and prune commands which include or exclude directories including all the files under a given directory. This can be useful for tests and documentation since they can include non-standard files. Beyond those examples, it's almost always better to explicitly include the files you need and ignore all the others. Here's an example MANIFEST.in:

# Comments can be added with a hash tag
include LICENSE CHANGES AUTHORS

# Include the docs, tests and examples completely
graft docs
graft tests
graft examples

# Always exclude compiled python files
global-exclude *.py[co]

# Remove documentation builds
prune docs/_build
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset