如何从GitHub安装具有额外依赖项但没有"git"的Python包



我正在寻找一个通过pip从GitHub安装Python包的一行程序,而不必安装git,并且能够指定额外的依赖项。

这是我尝试过的(因为我在Windows上,所以不需要逃离[]):

pip install https://github.com/python/mypy/archive/master.zip[reports]

下面的确实有效,第一个来自这个答案:

# requires a git client to be installed
pip install -e git+https://github.com/python/mypy.git#egg=mypy[reports]
# omits extra dependencies
pip install https://github.com/python/mypy/archive/master.zip

但正如评论中所指出的,他们有各种限制。

对于初始命令,我得到

Collecting https://github.com/python/mypy/archive/master.zip[reports]
ERROR: HTTP error 404 while getting https://github.com/python/mypy/archive/master.zip[reports]
ERROR: Could not install requirement https://github.com/python/mypy/archive/master.zip[reports] because of HTTP error 404 Client Error: Not Found for url: https://github.com/python/mypy/archive/master.zip%5Breports%5D for URL https://github.com/python/mypy/archive/master.zip[reports]

(我知道,由于https://github.com/pypa/setuptools_scm/blob/ca3855ba66fa4cb100f5039eea909932f815a4a4/src/setuptools_scm/__init__.py#L106-L115,这不应该适用于使用setuptools_scm的包,例如psf/black,但我的包并不依赖于此,并且可以从GitHub zip文件进行良好安装。)

我发现了一个相当模糊的解决方案

pip install "mypy[reports] @ https://github.com/python/mypy/archive/master.zip" 

from如何用轮子指定额外的pip安装?

还有什么更直观的吗?

我知道你要求的是一行代码,但让我首先描述一下针对Git存储库(不仅仅是Github)的常见解决方案,它也避免了需要Git:

  1. 下载源代码副本
    • 使用wgetcurl或任何可用的shell或脚本实用程序
    • 从Github,您可以
      • 特定分支或提交或标记的.zip/.tar.gz
        例如,从mypy的Github,您可以获得最新的master、特定标记v0.950,或者选择任何其他发布标记
      • 来自发行版的.zip/.tar.gz
        例如,从black的Github,从22.3.0发行版页面,获取22.3.0源代码
  2. 将.zip/.tar.gz解压缩/解压缩到某个临时目录
  3. 从该临时目录运行
    pip install ./<temp directory>[<extra dependency]
    
    • pip安装软件包的位置也有同样的注意事项
    • 如果使用虚拟环境,请确保首先激活它
    • 或者使用python -m pip install ...语法来选择特定的环境和解释器

有了像这样针对Git回购的常规pip install

pip install git+https://github.com/python/mypy.git#egg=mypy[reports]
pip install "mypy[reports] @ https://github.com/python/mypy/archive/master.zip"

它自动地已经执行了步骤1和2。如果添加-v/--verbose选项,您会看到它首先从特定提交中克隆代码的副本,并将它们存储在某个临时目录中(我们通常不关心这个目录),然后从那里进行常规安装。

mypy样本:

$ pip install -v git+https://github.com/python/mypy.git#egg=mypy[reports]
...
Collecting mypy[reports]
Cloning https://github.com/python/mypy.git to /private/var/folders/3h/pdjwtnlx4p13chnw21rvwbtw0000gp/T/pip-install-d6gv_iva/mypy_fe678fe853cb45b68755c0c57dfcb757
...
Cloning into '/private/var/folders/3h/pdjwtnlx4p13chnw21rvwbtw0000gp/T/pip-install-d6gv_iva/mypy_fe678fe853cb45b68755c0c57dfcb757'
...
Building wheels for collected packages: mypy
Running command Building wheel for mypy (pyproject.toml)
running bdist_wheel
running build
running build_py
pin_version()
creating build
creating build/lib
creating build/lib/mypy
creating build/lib/mypyc
...
Successfully built mypy
Installing collected packages: mypy
Running command git rev-parse HEAD
4f07c79aea0fef61ab649d6acedf01186f1054eb
changing mode of /path/to/venv/bin/dmypy to 755
changing mode of /path/to/venv/bin/mypy to 755
changing mode of /path/to/venv/bin/mypyc to 755
changing mode of /path/to/venv/bin/stubgen to 755
changing mode of /path/to/venvi/bin/stubtest to 755
Successfully installed mypy-0.960+dev.4f07c79aea0fef61ab649d6acedf01186f1054eb
$ python
...
>>> import importlib_metadata
>>> importlib_metadata.metadata('mypy').get_all('Requires-Dist')
[ ... extra == 'reports'"]

因此,如果你没有Git,你就不能做git clone,而是必须手动下载代码。与直接的pip install一样,如果您不关心特定的版本,您可以下载最新的master/maincommit:

再次为mypy取样:

$ wget -O mypy-master.zip https://github.com/python/mypy/archive/refs/heads/master.zip
...
2022-05-05 20:29:07 (2.85 MB/s) - ‘mypy-master.zip’ saved [3433301]
$ unzip -o mypy-master.zip
...
inflating: mypy-master/test-data/unit/typexport-basic.test  
inflating: mypy-master/test-requirements.txt  
inflating: mypy-master/tox.ini 

最后一步就是执行pip install,它支持以<local project path>为目标,并在一个命令中指定额外的内容:

python -m pip install .[PDF]  # project in current directory

因此,按照上面下载和解压的mypy-master,最后一步就是:

$ pip install -v ./mypy-master[reports]
Processing ./mypy-master
...
Building wheels for collected packages: mypy
Running command Building wheel for mypy (pyproject.toml)
running bdist_wheel
running build
running build_py
pin_version()
creating build
creating build/lib
creating build/lib/mypy
creating build/lib/mypyc
...
Successfully built mypy
Installing collected packages: mypy
Running command git rev-parse HEAD
4f07c79aea0fef61ab649d6acedf01186f1054eb
changing mode of /path/to/venv/bin/dmypy to 755
changing mode of /path/to/venv/bin/mypy to 755
changing mode of /path/to/venv/bin/mypyc to 755
changing mode of /path/to/venv/bin/stubgen to 755
changing mode of /path/to/venvi/bin/stubtest to 755
Successfully installed mypy-0.960+dev.4f07c79aea0fef61ab649d6acedf01186f1054eb
$ python
...
>>> import importlib_metadata
>>> importlib_metadata.metadata('mypy').get_all('Requires-Dist')
[ ... extra == 'reports'"]

其输出与CCD_ 20直接从CCD_。使用pip install的注意事项也适用,例如,如果您使用的是虚拟环境,请确保首先激活该环境,以便mypy安装在同一位置。

现在,为了使其成为一个单行命令,您可以将这些步骤转换为脚本、shell别名,或者使用&&操作符直接运行多个命令:

$ wget -O mypy-master.zip https://github.com/python/mypy/archive/refs/heads/master.zip && unzip -o mypy-master.zip && pip install ./mypy-master[reports] && rm -R mypy-master*

请注意,我在命令末尾添加了删除临时文件/文件夹。如果其中一个步骤失败(例如网络连接错误),则其余命令将中止。您可以根据需要添加更好/更多的错误处理。

$ wget -O mypy-master.zip https://github.com/python/mypy/archive/refs/heads/master.zip && unzip -o mypy-master.zip && pip install ./mypy-master[reports] && rm -Rf mypy-master*
--2022-05-05 21:09:39--  https://github.com/python/mypy/archive/refs/heads/master.zip
Resolving github.com (github.com)... failed: nodename nor servname provided, or not known.
wget: unable to resolve host address ‘github.com’

不过,我同意不是一个优雅或直观的命令。

最新更新