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

构建Python包时提示requirements.txt不存在的问题求助

解决Python打包时找不到requirements.txt的问题

问题场景

运行python3 -m build打包上传PyPI时,报错FileNotFoundError: [Errno 2] No such file or directory: 'requirements.txt',但本地目录确实存在该文件;添加-s参数打包后,从PyPI安装模块仍出现相同错误。

解决方案

1. 确保requirements.txt被包含在打包文件中

build过程会创建临时构建环境,如果requirements.txt未被声明为要包含的文件,临时环境里就找不到它:

  • 创建或修改MANIFEST.in文件,添加以下内容:
    include requirements.txt
    
  • 若使用setup.py,需开启include_package_data=True确保文件被打包:
    from setuptools import setup
    
    setup(
        # 其他项目配置
        include_package_data=True,
        install_requires=[line.strip() for line in open('requirements.txt').readlines()]
    )
    

2. 改用pyproject.toml直接声明依赖(推荐)

避免依赖外部文件,直接在pyproject.toml中定义项目依赖,build过程无需读取requirements.txt:

[project]
name = "your-package-name"
version = "0.1.0"
dependencies = [
    "requests>=2.25.1",
    "numpy>=1.21.0",
    # 将requirements.txt中的依赖逐一迁移到此处
]

3. 清理缓存后重新构建

残留缓存可能导致旧配置干扰,先清理再重新打包:

  • Windows命令:
    rmdir /s /q dist build *.egg-info
    
  • Linux/macOS命令:
    rm -rf dist build *.egg-info
    

执行清理后重新构建:

python3 -m build

内容的提问来源于stack exchange,提问作者LeiRoF

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 13:30:53