簡體   English   中英

如何從帶有 pip 的 git-lfs 內容的 git repo 安裝 python 包?

[英]How to install python package from git repo that has git-lfs content with pip?

我已將 git 存儲庫中的大文件遷移到 git-lfs。 該存儲庫包含自定義 python 庫的源代碼。 我能夠用 pip 安裝它:

pip install git+https://gitserver.com/myrepo.git@branch

目前(遷移后),存儲在 lfs 的大文件,顯然已經不在安裝了(只有鏈接)。 我已經在環境中從 PyPI 安裝了 git-lfs 包,但它沒有幫助。
有沒有辦法告訴 pip 在克隆 repo 時獲取 git-lfs 文件?

我遇到了同樣的問題,我有幾個包含 LFS 文件的內部 python 包。 他們每個人都有自己的requirements.txtsetup.py文件,其中一個基於pip並且不能很好地處理git lfs ,另一個基於setuptools ...

我的requirements.txt看起來像:

pip_package1==0.1
pip_package2==0.2
pip_package3==0.3
git+ssh://git@{url}:{port}/{repo}.git@{branch}

setup.py一個解決方案是捕獲任何git+ssh以不讓它們進入setup函數,而是通過 pip 強制它們安裝:

def install_requires():
    reqdir = os.path.dirname(os.path.realpath(__file__))
    with open(os.path.join(reqdir, 'requirements.txt'), encoding='utf-8') as f:
        all_packages = f.readlines()
        packages = [
            package
            for package in all_packages
            if 'git+ssh' not in package
        ]
        manual_pip_packages = [
            package
            for package in all_packages
            if 'git+ssh' in package
        ]
        for package in manual_pip_packages:
            subprocess.call([sys.executable, '-m', 'pip', 'install', package])
    return packages

然后是pip / git lfs兼容性。 我的理解是 git repo 上的pip install將簡單地git clone然后python setup.py [something]所以我在包含 lfs 文件的包中的每個setup.py添加了一個git pull假設相關的setup.py在一個git 存儲庫。

def pull_first():
    """This script is in a git directory that can be pulled."""
    cwd = os.getcwd()
    gitdir = os.path.dirname(os.path.realpath(__file__))
    os.chdir(gitdir)
    g = git.cmd.Git(gitdir)
    try:
        g.execute(['git', 'lfs', 'pull'])
    except git.exc.GitCommandError:
        raise RuntimeError("Make sure git-lfs is installed!")
    os.chdir(cwd)

這樣我就有了一個setup.py

from setuptools import setup, find_packages

import os
import subprocess
import sys

try:
    import git
except ModuleNotFoundError:
    subprocess.call([sys.executable, '-m', 'pip', 'install', 'gitpython'])
    import git


def install_requires():
    ...


def pull_first():
    ...


pull_first()

setup(name=...,
      version=...,
      description=...,
      long_description=...,
      url=...,
      author=...,
      license=...,
      packages=find_packages(),
      python_requires=">=3.0",
      install_requires=install_requires())

如果您正確設置了每用戶或系統配置設置,那么在克隆使用 Git LFS 的存儲庫時,Git 將自動調用 Git LFS。

最簡單的方法是運行git lfs install --skip-repo ,這將修改您的.gitconfig以包含正確的條目。 您可以通過運行git lfs env並確保打印的最后三個git config選項非空來驗證您的配置是否正確。

設置完成后,任何時候您使用 Git LFS 克隆新存儲庫時,LFS 文件都會自動獲取並填充。如果您有現有存儲庫,則可以使用git lfs checkout手動git lfs checkout文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM