Python打包时的MANIFEST.in如何使用

发布时间 2023-08-18 13:04:57作者: tangjicheng

MANIFEST.in 文件用于控制在 Python 包的源码分发中应该包含哪些文件和目录。这对于包含在源码分发中但不是 Python 源代码文件的文件(如文档、配置文件、数据文件等)特别有用。

以下是 MANIFEST.in 中的主要指令及其描述:

  1. include:包含匹配指定模式的文件。

    include example.txt
    
  2. exclude:排除匹配指定模式的文件。

    exclude example.txt
    
  3. recursive-include:递归地包含匹配指定模式的文件。

    recursive-include dir_name *.txt
    
  4. recursive-exclude:递归地排除匹配指定模式的文件。

    recursive-exclude dir_name *.txt
    
  5. graft:包含指定目录及其所有内容。

    graft dir_name
    
  6. prune:排除指定目录及其所有内容。

    prune dir_name
    
  7. global-include:包含所有匹配指定模式的文件。

    global-include *.txt
    
  8. global-exclude:排除所有匹配指定模式的文件。

    global-exclude *.pyc
    

除了上述指令,你还可以使用注释来增加文件的可读性。注释以 # 开始并持续到行尾。

示例 MANIFEST.in 文件:

# Include all txt files
global-include *.txt

# Exclude all pyc files
global-exclude *.pyc

# Include all files in the docs directory
graft docs

# Exclude all txt files in the docs directory
recursive-exclude docs *.txt

这些指令允许你精确地控制应该包含或排除哪些文件,从而确保你的源码分发只包含所需的文件。