How can I exclude specific directories or files from being included in a Python package when building it for distribution?

Asked 6 months ago

I'm building a Python package for distribution, but I need to exclude certain directories and files from being included. What techniques and configurations should I use to ensure these unwanted components are not part of the final package?

Filip Dimkovski

Friday, November 03, 2023

You can follow these simple steps to exclude specific directories and files from your Python package:

  1. Create `.gitignore` File: Begin by creating a `.gitignore` file in the root directory of your project. In this file, you can list the names of the directories or files you want to exclude. These entries prevent them from being tracked by version control systems like Git.
  2. Use `MANIFEST.in`: Create or modify the `MANIFEST.in` file in your project's root directory, which is a file that allows you to specify inclusion or exclusion rules for files and directories when building the package.
  3. Leverage Patterns and Wildcards: Within the `MANIFEST.in` file, you can use patterns and wildcards to define more complex rules. For example, if you want to exclude all files with a specific extension, you can use a wildcard like `*.log` to match all log files.
  4. Reference `MANIFEST.in` in `setup.py`: To ensure that these rules are applied during the package creation process, make sure your `setup.py` script references the `MANIFEST.in` file. This step ensures that the package builder uses the defined rules when assembling the distribution package.




Write an answer...

Cancel

Please follow our  Community Guidelines

Can't find what you're looking for?