You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从TFS2017通过pip安装时,如何在Python包中包含.ini文件?

如何将Python库中的.ini配置文件正确打包发布?

项目目录结构

├───src
│   ├───main
│   │   │   MANIFEST.in
│   │   │   setup.py
│   │   │
│   │   ├───matmuttools
│   │   │   │   matmut_logging.py
│   │   │   │   proxy.py
│   │   │   │   PROXY_USAGE.md
│   │   │   │   singleton.py
│   │   │   │   __init__.py
│   │   │   │
│   │   │   ├───ql
│   │   │   │   │   default_config.ini    <---- 需要打包的配置文件
│   │   │   │   │   query_language.py
│   │   │   │   │   tsql.py
│   │   │   │   │   __init__.py

已尝试的无效操作

  • 在MANIFEST.in中添加以下配置:
    include matmuttools/ql/default_config.ini
    
  • 在setup.py中添加以下配置:
    include_package_data=True,
    package_data={'matmuttools': ['*.ini']}
    

文件未被打包的验证

  1. 运行时报错(configparser找不到配置节):
    File "/usr/local/lib/python3.9/site-packages/matmuttools/ql/query_language.py", line 41, in __init__
        raise ValueError(f"{self.language_name} not found in config file")
    ValueError: TSQL not found in config file
    
  2. 安装后查看包目录,无.ini文件:
    ls -lR /usr/local/lib/python3.9/site-packages/matmuttools/*
    
    /usr/local/lib/python3.9/site-packages/matmuttools/ql:
    total 16
    -rw-r--r-- 1 root root  120 Sep 30 08:44 __init__.py
    drwxr-xr-x 2 root root 4096 Sep 30 08:44 __pycache__
    -rw-r--r-- 1 root root 2159 Sep 30 08:44 query_language.py
    -rw-r--r-- 1 root root  478 Sep 30 08:44 tsql.py
    
  3. 构建的wheel包中也无该文件:
    $ zipinfo dist/matmuttools-3.2.0-py3-none-any.whl 
    Archive:  dist/matmuttools-3.2.0-py3-none-any.whl
    Zip file size: 12646 bytes, number of entries: 14
    -rw-rw-rw-  2.0 fat      153 b- defN 22-Sep-29 10:12 matmuttools/__init__.py
    -rw-rw-rw-  2.0 fat     9841 b- defN 22-Jun-13 06:37 matmuttools/matmut_logging.py
    -rw-rw-rw-  2.0 fat     2047 b- defN 22-Jun-13 06:37 matmuttools/proxy.py
    -rw-rw-rw-  2.0 fat      928 b- defN 22-Jun-13 06:37 matmuttools/singleton.py
    -rw-rw-rw-  2.0 fat      257 b- defN 22-Jun-13 06:37 matmuttools/hadoop/__init__.py
    -rw-rw-rw-  2.0 fat     9891 b- defN 22-Jun-13 06:37 matmuttools/hadoop/hdfs_wrapper.py
    -rw-rw-rw-  2.0 fat    10465 b- defN 22-Jun-13 06:37 matmuttools/hadoop/impala_wrapper.py
    -rw-rw-rw-  2.0 fat      120 b- defN 22-Sep-29 09:22 matmuttools/ql/__init__.py
    -rw-rw-rw-  2.0 fat     2159 b- defN 22-Sep-29 12:19 matmuttools/ql/query_language.py
    -rw-rw-rw-  2.0 fat      478 b- defN 22-Sep-29 11:56 matmuttools/ql/tsql.py
    -rw-rw-rw-  2.0 fat      454 b- defN 22-Sep-30 09:08 matmuttools-3.2.0.dist-info/METADATA
    -rw-rw-rw-  2.0 fat       97 b- defN 22-Sep-30 09:08 matmuttools-3.2.0.dist-info/WHEEL
    -rw-rw-rw-  2.0 fat       12 b- defN 22-Sep-30 09:08 matmuttools-3.2.0.dist-info/top_level.txt
    ?rw-rw-r--  2.0 fat     1155 b- defN 22-Sep-30 09:08 matmuttools-3.2.0.dist-info/RECORD
    14 files, 38057 bytes uncompressed, 10720 bytes compressed:  71.8%
    
    注:即使在MANIFEST.in中添加prune matmuttools/ql,wheel包内容也无变化,说明MANIFEST.in未被正确识别。

正确解决方案

1. 修正setup.py中的package_data配置

.ini文件属于matmuttools.ql子包,而非根包,因此需要精准指定路径:

from setuptools import setup, find_packages

setup(
    # 其他配置项...
    packages=find_packages(),  # 确保包含所有子包(包括matmuttools.ql)
    include_package_data=True,
    package_data={
        'matmuttools.ql': ['default_config.ini']  # 明确指定子包下的.ini文件
    }
)

2. 确保MANIFEST.in路径正确(可选,但推荐用于源码包)

在MANIFEST.in中添加:

include matmuttools/ql/default_config.ini

注意路径是相对于setup.py的位置(当前setup.py在src/main目录下,该路径是正确的)。

3. 使用正确的工具构建包

放弃python setup.py bdist_wheel,改用官方推荐的build模块:

# 先安装build模块
pip install build
# 构建wheel和源码包
python -m build

4. 正确读取配置文件

不能用相对路径直接读取,需通过Python的资源读取API来获取包内文件:

import configparser
from importlib.resources import read_text

# 读取matmuttools.ql包下的default_config.ini
config_content = read_text('matmuttools.ql', 'default_config.ini')

# 解析配置
config = configparser.ConfigParser()
config.read_string(config_content)

# 验证配置节
if 'TSQL' not in config:
    raise ValueError("TSQL not found in config file")

内容的提问来源于stack exchange,提问作者Itération 122442

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 19:45:41