如何使用额外的依赖项安装GitHub的Python软件包,但没有“ git”?

发布于 2025-01-26 10:21:10 字数 1926 浏览 3 评论 0 原文

我正在寻找一个单线线,可以通过 pip 从Github安装Python软件包,而无需安装 git ,并且能够指定额外的依赖项。

这就是我尝试的(当我在Windows上时,无需逃脱 [] ):

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

以下 di di di di di 工作,第一个从此答案

# 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]

(我知道这不应使用 setuptools_scm ,例如, psf/black ,因此不应适用于包装。 “ https://github.com/pypa/setuptools_scm/blob/ca38555ba66fa4cb100f5039eea909932f815a4a4a4/src/setsrc/setsrc/setuptools_scm/__init __init__init__init__init__init_.py#l106-l106-l106-l106-l115 re =” UpTools_scm . py#l106-l115

/blob/ca38555ba666fa4cb100f5039eea909932f815a4a4a4/src/strc/setuptoopools_scm /__ init __

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

来自

有什么更直观的吗?

I am looking for a one-liner to install a Python package from GitHub via pip, without having git installed necessarily, and with being able to specify extra dependencies.

This is what I tried (no need to escape [] as I am on Windows):

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

The following does work, with the first one taken from this answer:

# 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

But they have various restrictions as noted in the comments.

For the initial command, I am getting

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]

(I am aware that this is not supposed to work for packages using setuptools_scm, e.g., psf/black, due to https://github.com/pypa/setuptools_scm/blob/ca3855ba66fa4cb100f5039eea909932f815a4a4/src/setuptools_scm/__init__.py#L106-L115, but my package does not depend on that and installs fine from a GitHub zip file.)

I have found the rather obscure solution

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

from How to pip install with wheel specifying extras?

Is there anything more intuitive?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙气飘飘 2025-02-02 10:21:10

我知道您是在要求一个单线,但让我首先描述针对GIT存储库(不仅仅是GitHub)的常见解决方法,该解决方案也避免了需要Git:

  1. 下载源代码的副本
    • 使用 WGET curl 或任何可以使用的shell或脚本实用程序
    • 来自github,您可以针对
  2. ,获取 将.zip/.tar.gz
  3. 从该临时目录运行到某个临时目录,运行
      pip install ./ 

M PIP安装... 语法使用常规 pip install 以这样的git回购

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

, ... 2。如果添加a / - 详细信息选项,您会看到它首先从特定的提交中夹住代码的副本,然后将它们存储在某些临时目录中(我们不适合通常关心),然后从那里进行定期安装。

示例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克隆,而是必须手动下载代码。与Direct PIP Install 一样,如果您不在乎特定版本,则可以下载最新的 Master / main commit:

再次为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> and 指定额外功能所有命令中的全部命令:

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

因此,遵循下载和拆卸 mypy> 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'"]

...显示与 pip install -ED直接从 git+https 的输出相同的输出。使用 pip install 使用相同的注意事项,例如,如果您使用的是虚拟环境,请确保首先激活该问题,以便将mypy安装在同一位置。

现在,为了使其成为一个单线命令,您可以将这些步骤转换为脚本,外壳别名,或者只是直接上升使用&& 操作员运行多个命令

$ 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’

我同意 不是优雅或直观的命令。

I know you are asking for a one-liner, but let me describe first the common workaround to targeting Git repositories (not just Github) that also avoids requiring git:

  1. Download a copy of the source code
    • Using wget or curl or whichever shell or script-able utility is available
    • From Github, you can target either
  2. Unzip/Untar the .zip/.tar.gz to some temporary directory
  3. From that temporary directory, run
    pip install ./<temp directory>[<extra dependency]
    
    • The same considerations for where pip installs the package applies
    • If using a virtual env, make sure to activate it first
    • Or use the python -m pip install ... syntax to select a specific environment and interpreter

With regular pip install targeting a Git repo like this,

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

...it automatically already does steps 1 and 2. If you add a -v/--verbose option, you'll see that it clones a copy of the codes first from a specific commit, and stores them in some temporary directory (which we don't normally care about), then does the regular installation from there.

Sample for 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'"]

So, if you don't have Git, you can't do a git clone, but instead have to download the codes manually. As with the direct pip install, if you don't care about a specific version, you can just download the latest master/main commit:

Sample again for 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 

Then the last step would simply be doing pip install, which supports targetting a <local project path> and specifying extras all in one command:

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

So following the downloaded and unpacked mypy-master above, the last step would simply be:

$ 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'"]

...which shows the same output as the one that pip install-ed directly from git+https. The same considerations with using pip install applies, for example, if you are using a virtual environment, make sure to activate that first so mypy is installed in the same location.

Now, to make it all a one-liner command, you can turn those steps into a script, a shell alias, or just straight-up run multiple commands with && operator:

$ 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*

Note that I added removal of the temporary files/folders at the end of the command. If one of the steps fails (ex. network connection error), then the rest of the commands will be aborted. You can add better/more error-handling as needed.

$ 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’

I agree though that that isn't an elegant or an intuitive command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文