【问题标题】:List dependencies of Python wheel file列出 Python Wheel 文件的依赖项
【发布时间】:2018-10-14 16:26:47
【问题描述】:

我有 Python 轮子文件:psutil-5.4.5-cp26-none-linux_x86_64.whl

如何列出这个轮子的依赖关系?

【问题讨论】:

  • 也许你正在寻找类似this的东西?
  • @Begueradj 不是,您链接的问题不同。我想从轮文件中提取依赖项。轮子尚未安装。
  • 实际上,我只是尝试解压缩(而不是 gunzip)我躺在身边的一个轮子,并且在 packagename-version.dist-info/METADATA 文件中包含一个 Requires-Dist: 条目列表,其中包含来自 setup.py 的编译要求.
  • @ErikCederstrand 谢谢!如果你把这个写成答案,那么我会投票赞成。
  • 你可以试试这个$ pip install pipdeptree 然后$ pipdeptree -fl

标签: python python-wheel


【解决方案1】:

从您解压缩 Wheel 文件的目录(将 .whl 更改为 .zip 并解压缩)在命令行的 shell 中运行以下命令:

grep --include=METADATA -rnw '.' -e "Requires-Dist"

【讨论】:

    【解决方案2】:

    您可以在单独的虚拟环境中安装wheel文件,然后查看安装了哪些所有其他软件包。

    使用pip freeze 命令查看所有已安装的包。

    【讨论】:

    • 这是使用pip list 命令查看递归需求(其他包需要的包)的唯一方法。
    • 但这并不能说明是否需要某些版本的软件包。
    • @RolandWeber 如果在安装其他包的过程中安装了一个包,则意味着它是必需的。
    • @SHIVAMJINDAL 表示需要该包,但不知道是否需要或排除某些版本的包。 xyz==3.5.7, xyz>3.0, xyz
    • @RolandWeber 会告诉你安装父包的时候安装了什么版本。
    【解决方案3】:

    如前所述,.whl 文件只是 ZIP 档案。您可以打开它们并在 METADATA 文件中四处寻找。

    不过,有一个工具可以让这个手动过程变得更容易一些。可以使用pkginfo,可以用pip安装。

    CLI 用法:

    $ pip install pkginfo
    $ pkginfo -f requires_dist psutil-5.4.5-cp27-none-win32.whl
    requires_dist: ["enum34; extra == 'enum'"]
    

    API 使用:

    >>> import pkginfo
    >>> wheel_fname = "psutil-5.4.5-cp27-none-win32.whl"
    >>> metadata = pkginfo.get_metadata(wheel_fname)
    >>> metadata.requires_dist
    [u"enum34 ; extra == 'enum'"]
    

    【讨论】:

      【解决方案4】:

      我使用pipenv 安装我的虚拟环境,安装pew 作为要求。 pew 允许您安装临时虚拟环境,这些虚拟环境会在您 exit 时删除那些特殊的虚拟环境。所以...

      创建一个新的空虚拟环境并即时激活它:

      pew mktmpenv -p /usr/bin/python3.6

      安装你的包:

      pip install somedistro

      查看您的发行版的要求是什么(以及要求的要求...):

      pip list

      停用并删除临时环境。

      exit

      此外,临时虚拟环境在打包测试中非常有用。

      【讨论】:

        【解决方案5】:

        这是我在某处找到的帖子。我复制了所有内容并给自己发了电子邮件,所以我没有这个答案的来源,但我认为这可能会有所帮助。如果有人知道出处,我会删除这个帖子并链接到那个帖子。

        Installation
        $ pip install --upgrade pip  # pip-tools needs pip==6.1 or higher (!)
        $ pip install pip-tools
        Example usage for pip-compile
        Suppose you have a Flask project, and want to pin it for production. Write the following line to a file:
        
        # requirements.in
        Flask
        Now, run pip-compile requirements.in:
        
        $ pip-compile requirements.in
        #
        # This file is autogenerated by pip-compile
        # Make changes in requirements.in, then run this to update:
        #
        #    pip-compile requirements.in
        #
        flask==0.10.1
        itsdangerous==0.24        # via flask
        jinja2==2.7.3             # via flask
        markupsafe==0.23          # via jinja2
        werkzeug==0.10.4          # via flask
        And it will produce your requirements.txt, with all the Flask dependencies (and all underlying dependencies) pinned. Put this file under version control as well and periodically re-run pip-compile to update the packages.
        
        Example usage for pip-sync
        Now that you have a requirements.txt, you can use pip-sync to update your virtual env to reflect exactly what's in there. Note: this will install/upgrade/uninstall everything necessary to match the requirements.txt contents.
        
        $ pip-sync
        Uninstalling flake8-2.4.1:
          Successfully uninstalled flake8-2.4.1
        Collecting click==4.1
          Downloading click-4.1-py2.py3-none-any.whl (62kB)
            100% |████████████████████████████████| 65kB 1.8MB/s
          Found existing installation: click 4.0
            Uninstalling click-4.0:
              Successfully uninstalled click-4.0
        Successfully installed click-4.1
        

        requirements.txt 将包含软件包所需的所有要求。

        【讨论】:

        • 我放弃了使用pip-tools 转而使用pipenv,它在该领域做得更好(接近于npm 对Javascript 的作用)。
        【解决方案6】:

        这是一个不需要任何外部工具(解压缩、gzip 或类似工具)的最小 sn-p,因此它应该在 *nix/windows 中都可以工作:

        wheeldeps.py:

        import argparse
        from zipfile import ZipFile
        
        parser = argparse.ArgumentParser()
        parser.add_argument('filename')
        args = parser.parse_args()
        
        archive = ZipFile(args.filename)
        for f in archive.namelist():
            if f.endswith("METADATA"):
                for l in archive.open(f).read().decode("utf-8").split("\n"):
                    if 'requires-dist' in l.lower():
                        print(l)
        

        例子:

        > python wheeldeps.py psutil-5.4.5-cp27-cp27m-win_amd64.whl
        Requires-Dist: enum34; extra == 'enum'  
        

        【讨论】:

          【解决方案7】:

          我只是试图解压缩(不是 gunzip)我放在身边的一个轮子包。 packagename-version.dist-info/METADATA 文件包含 Requires-Dist: 条目列表,其中包含来自 setup.py 的编译要求。

          【讨论】:

            猜你喜欢
            • 2021-10-23
            • 1970-01-01
            • 2023-03-03
            • 2023-03-23
            • 2017-07-03
            • 1970-01-01
            • 1970-01-01
            • 2014-11-03
            相关资源
            最近更新 更多