【问题标题】:How to escape spaces in path to local git repo when specifying dependency in requirements.txt在requirements.txt中指定依赖项时如何转义本地git repo路径中的空格
【发布时间】:2019-10-02 11:55:37
【问题描述】:

如何在 requirements.txt 中指定一个本地 git repo 作为依赖项,以便我可以使用pip install -r requirements.txt 安装它?问题不在于我无法从该位置安装或克隆,而是如何将路径中的空格转义到 repo 目录在 requirements.txt 中。要完全清楚,克隆和 pip 安装都通过将路径包含在 " 中来工作,它只会在 pip install -r 中失败。

我试过了

  • git+"K:/my/path with/lots of/spaces/repo/.git"

  • git+git://"K:/my/path with/lots of/spaces/repo/.git"

  • git+git:"//K:/my/path with/lots of/spaces/repo/.git"

  • git+"git://K:/my/path with/lots of/spaces/repo/.git"

  • "K:/my/path with/lots of/spaces/repo/.git"

  • git+file://K:/my/path\ with/lots\ of/spaces/repo/.git

但在每种情况下,我都会收到错误 ValueError: No closing quotation

如果我尝试使用 \ 转义空格,它会被解释为 Windows 路径分隔符并失败。

编辑

所以下面的工作:

pip install git+file:///"k/my/path with/lots of/spaces/repo/.git/"

但是,当我将完全相同的内容放入我的requirements.txt(当然没有pip install)时,我得到了ValueError: No closing quotation。任何关于如何解决这个问题的想法都将受到高度赞赏。我应该为 pip 提出问题吗?

【问题讨论】:

  • 你读过docs。您至少需要一个分支名称。
  • 如果我指定一个分支名称,我会得到同样的错误。我可以使用git clone "K:/my/path with/lots of/spaces/repo/.git" 克隆存储库。这是怎么复制的?这根本不是一个特定的分支。
  • @PedroRodrigues 不是重复的:这涉及本地文件路径,而不是来自 github 或 bitbucket(该问题的所有答案都显示)。
  • 您的意思是在需求文件中使用时损坏了?因为我这边的测试并没有表明这一点。

标签: python git pip


【解决方案1】:

我在这里引用用户 chrahunt,他在 GitHub 上回复了issue

直接执行 pip 时对引号的处理由您的 shell 控制。例如,在 Bash 中,非嵌套的引号对被剥离,它们的内容与之前/之后的内容连接起来以创建每个参数。

另一方面,要求文件由 optparse 按原样读取,不会进行特殊的报价处理或剥离。

也就是说,在这种情况下,当尝试提供 URL 时,无效的 URL 字符(如空格)必须是百分比编码的(空格是 %20)。

所以在我的情况下,我不得不将 requirements.txt 条目更改为 git+file:///k/my/path%20with/lots%20of/spaces/repo/.git/ 并且它起作用了。这不应该依赖于平台,%20 应该在任何地方都可以工作。

【讨论】:

    猜你喜欢
    • 2017-04-10
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    相关资源
    最近更新 更多