【问题标题】:Configuring so that pip install can work from github配置以便 pip install 可以从 github 工作
【发布时间】:2012-01-05 01:33:13
【问题描述】:

我们想使用 pip 和 github 将私有包安装到我们的生产服务器。这个问题涉及 github 存储库中需要哪些内容才能成功安装。

假设以下命令行(验证正常并尝试安装):

pip install git+ssh://git@github.com/BlahCo/search/tree/prod_release_branch/ProductName

ProductName 中需要包含哪些内容?是使用 sdist 选项运行 setup.py 后 tar 文件中通常包含的内容,还是实际的 tar.gz 文件或其他内容?

我在这里问是因为我尝试了几种变体,但都无法奏效。任何帮助表示赞赏。

【问题讨论】:

    标签: python git pip


    【解决方案1】:

    你需要整个 python 包,里面有一个setup.py 文件。

    名为foo 的包将是:

    foo # the installable package
    ├── foo
    │   ├── __init__.py
    │   └── bar.py
    └── setup.py
    

    并从 github 安装,如:

    $ pip install git+ssh://git@github.com/myuser/foo.git
    or
    $ pip install git+https://github.com/myuser/foo.git@v123
    or
    $ pip install git+https://github.com/myuser/foo.git@newbranch
    

    更多信息https://pip.pypa.io/en/stable/reference/pip_install/#vcs-support

    【讨论】:

    • 以上工作,非常感谢。但是,如果我在 repo 的子目录中有发布,那么我正在寻找 foo/releases/ProductVer 而不是 foo.git 。这可能吗?如果可以,怎么办?非常感谢您的帮助!
    • 不,这是不可能的。 pip 仅从根存储库目录安装,至少对于 git。不知道 subversion 的行为如何......
    • 如果您想通过 ssh 和私有仓库执行此操作,this is a post on how to do that
    • 为什么没有将-e选项(可编辑模式)传递给pip?
    • 这是新的 url 方案:pip install git+https://github.com/pypa/pip.git 来源:pip Github repo
    【解决方案2】:

    当我不得不从 github repo 安装但不想安装 git 等时,我遇到了类似的问题。

    简单的方法是使用压缩包的压缩包。将/zipball/master 添加到repo URL:

        $ pip install https://github.com/hmarr/django-debug-toolbar-mongo/zipball/master
    Downloading/unpacking https://github.com/hmarr/django-debug-toolbar-mongo/zipball/master
      Downloading master
      Running setup.py egg_info for package from https://github.com/hmarr/django-debug-toolbar-mongo/zipball/master
    Installing collected packages: django-debug-toolbar-mongo
      Running setup.py install for django-debug-toolbar-mongo
    Successfully installed django-debug-toolbar-mongo
    Cleaning up...
    

    这样您就可以让 pip 与 github 源代码库一起工作。

    【讨论】:

    • 这是唯一适用于 Windows 的答案
    • 如何使用私有存储库做到这一点? (根据问题)
    【解决方案3】:

    如果你想使用requirements.txt 文件,你需要git 和类似下面的条目来匿名获取requirements.txt 中的主分支。

    对于常规安装:

    git+git://github.com/celery/django-celery.git
    

    对于“editable”安装:

    -e git://github.com/celery/django-celery.git#egg=django-celery
    

    可编辑模式将项目的源代码下载到当前目录下的./src。它允许pip freeze 输出包的正确github位置。

    【讨论】:

    • 鸡蛋的名字从何而来?用github repo替换pip包后无法在django中加载包
    • @holms 在 setup.py 中查找名称
    • #egg=django-celery 是什么?那是占位符吗?
    • @enchance 是包名。
    • 我发现 git+git://github.com 在 Windows 上不起作用(pip 在克隆时失败)但 git+https://github.com 起作用了。
    【解决方案4】:

    像克隆任何其他项目一样克隆目标存储库:

    git clone git@github.com:myuser/foo.git
    

    然后在开发模式下安装:

    cd foo
    pip install -e .
    

    您可以更改任何您不想更改的内容,并且使用 foo 包的每个代码都将使用修改后的代码。

    此解决方案有 2 个好处:

    1. 您可以在您的主项目目录中安装包。
    2. 包包含.git 目录,因此它是常规的Git 存储库。你可以马上推到你的叉子上。

    【讨论】:

    • 我可以证明这个解决方案很神奇。就我而言,我想破解pip,所以我克隆了pip 存储库,创建了一个virtualenv,激活了它,然后做了pip install -e .。然后virtualenv中的pip处于开发模式!令我印象深刻的是,这甚至适用于包管理器本身。
    • 这太棒了!我注意到当我使用这种方法安装然后运行pip list 时,有问题的包引用了 Git 分支和安装它的绝对路径。它是否保留对这些的任何引用,或者可以删除源?
    【解决方案5】:

    这是简单的解决方案

    使用 git

    pip install git+https://github.com/jkbr/httpie.git
    

    没有 git

    pip install https://github.com/jkbr/httpie/tarball/master
    

    pip install https://github.com/jkbr/httpie/zipball/master  
    

    pip install https://github.com/jkbr/httpie/archive/master.zip
    

    注意:您需要一个包含 setup.py 文件的 python 包。

    【讨论】:

      【解决方案6】:

      你可以在 Colab 中尝试这种方式

      !git clone https://github.com/UKPLab/sentence-transformers.git
      !pip install -e /content/sentence-transformers
      import sentence_transformers
      

      【讨论】:

        【解决方案7】:

        以下格式可用于通过pipGitHub 安装python 库。

        pip install <LibName>@git+ssh://git@github.com/<username>/<LibName>#egg<LibName>
        

        【讨论】:

          猜你喜欢
          • 2012-05-14
          • 2012-04-14
          • 2021-09-29
          • 1970-01-01
          • 1970-01-01
          • 2014-03-09
          • 2015-01-03
          • 1970-01-01
          相关资源
          最近更新 更多