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

如何通过requirements.txt和setup.py从Git下载最新.py文件供其他仓库使用

从Git仓库指定路径下载单个.py文件的实现方案

一、使用requirements.txt实现

直接将目标文件的Raw URL写入requirements.txt即可,该URL指向Git仓库指定分支的最新版本文件:

https://git.myorg.com/it/xyz/abc/-/raw/develop/myfile.py

下载执行方式

  • 如果要将文件安装到Python的site-packages目录(可直接导入),执行:
    pip install -r requirements.txt
    
  • 如果要将文件下载到项目指定目录(比如./deps),避免污染全局环境,执行:
    pip install -r requirements.txt --target ./deps
    

导入使用

若文件下载到自定义目录,需在代码中添加路径后导入:

import sys
sys.path.append("./deps")
import myfile

二、使用setup.py实现

在项目的setup.py中,将目标文件的Raw URL添加到install_requires列表,其他仓库安装你的包时会自动下载该文件:

from setuptools import setup

setup(
    name="your-project-name",
    version="0.1.0",
    install_requires=[
        "https://git.myorg.com/it/xyz/abc/-/raw/develop/myfile.py"
    ]
)

安装执行方式

其他仓库使用你的包时,执行以下命令即可自动下载依赖的myfile.py:

pip install path/to/your-project

注意事项

  • 上述配置均指向develop分支的最新版本,每次执行安装命令都会拉取该分支下对应路径的最新文件,无需绑定特定提交哈希。
  • 若为私有Git仓库,需在URL中嵌入身份凭证(如https://username:token@git.myorg.com/it/xyz/abc/-/raw/develop/myfile.py)以确保访问权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 15:34:43