查看已安装的Python包依赖树

如有侵权,请联系作者。
转载请注明出处。
帮助他人,记录自己。

目录

标签

Python 依赖管理,Python 包管理,Python,依赖管理,包管理,依赖树,依赖信息,pip,pipdeptree。

修订历史

  • 2020-04-24:首次编辑。

说明

Python 虽然有虚拟环境(virtualenv)工具可以方便管理项目之间的依赖,但是项目内的依赖树依然有些混乱,如果有类似 Java 中 Maven 的 dependency tree 可以一键查看所有依赖树信息就好了。于是在网上找到一款可以方便查看 Python 已安装包的依赖树信息查看工具 pipdeptree。使用方法如下:

使用

安装 pipdeptree

(temp-test) [adam@ZCY ~]$ pip install pipdeptree

Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting pipdeptree
  Downloading http://mirrors.aliyun.com/pypi/packages/12/64/26c7df3ad833cd6e8b9735c5958b25d6aef1617c915b2731baedfbeee712/pipdeptree-0.13.2-py3-none-any.whl (16 kB)
Requirement already satisfied: pip>=6.0.0 in ./virtualenv/temp-test/lib/python3.6/site-packages (from pipdeptree) (20.0.2)
Installing collected packages: pipdeptree
Successfully installed pipdeptree-0.13.2

查看 pipdeptree 版本

(temp-test) [adam@ZCY ~]$ pipdeptree -v

0.13.2

直接使用

(temp-test) [adam@ZCY ~]$ pipdeptree

Django==2.2.12
  - pytz [required: Any, installed: 2019.3]
  - sqlparse [required: Any, installed: 0.3.1]
pipdeptree==0.13.2
  - pip [required: >=6.0.0, installed: 20.0.2]
setuptools==45.2.0
wheel==0.34.2

用 freeze 格式显示依赖树

(temp-test) [adam@ZCY ~]$ pipdeptree -f

Django==2.2.12
  pytz==2019.3
  sqlparse==0.3.1
pipdeptree==0.13.2
  pip==20.0.2
setuptools==45.2.0
wheel==0.34.2

显示所有包的依赖树,包括依赖包和被依赖包

(temp-test) [adam@ZCY ~]$ pipdeptree -a

Django==2.2.12
  - pytz [required: Any, installed: 2019.3]
  - sqlparse [required: Any, installed: 0.3.1]
pip==20.0.2
pipdeptree==0.13.2
  - pip [required: >=6.0.0, installed: 20.0.2]
pytz==2019.3
setuptools==45.2.0
sqlparse==0.3.1
wheel==0.34.2

反转依赖树

(temp-test) [adam@ZCY ~]$ pipdeptree -r

pip==20.0.2
  - pipdeptree==0.13.2 [requires: pip>=6.0.0]
pytz==2019.3
  - Django==2.2.12 [requires: pytz]
setuptools==45.2.0
sqlparse==0.3.1
  - Django==2.2.12 [requires: sqlparse]
wheel==0.34.2

只显示某些包的依赖树

(temp-test) [adam@ZCY ~]$ pipdeptree -p django

Django==2.2.12
  - pytz [required: Any, installed: 2019.3]
  - sqlparse [required: Any, installed: 0.3.1]

不显示某些包的依赖树

(temp-test) [adam@ZCY ~]$ pipdeptree -e django

pipdeptree==0.13.2
  - pip [required: >=6.0.0, installed: 20.0.2]
setuptools==45.2.0
wheel==0.34.2

使用 Json 格式显示所有包的依赖树,包括依赖包和被依赖包

(temp-test) [adam@ZCY ~]$ pipdeptree -j

[
    { 
        "package": { 
            "key": "wheel",
            "package_name": "wheel",
            "installed_version": "0.34.2"
        },
        "dependencies": []
    },
    { 
        "package": { 
            "key": "sqlparse",
            "package_name": "sqlparse",
            "installed_version": "0.3.1"
        },
        "dependencies": []
    },
    { 
        "package": { 
            "key": "setuptools",
            "package_name": "setuptools",
            "installed_version": "45.2.0"
        },
        "dependencies": []
    },
    { 
        "package": { 
            "key": "pytz",
            "package_name": "pytz",
            "installed_version": "2019.3"
        },
        "dependencies": []
    },
    { 
        "package": { 
            "key": "pipdeptree",
            "package_name": "pipdeptree",
            "installed_version": "0.13.2"
        },
        "dependencies": [
            { 
                "key": "pip",
                "package_name": "pip",
                "installed_version": "20.0.2",
                "required_version": ">=6.0.0"
            }
        ]
    },
    { 
        "package": { 
            "key": "pip",
            "package_name": "pip",
            "installed_version": "20.0.2"
        },
        "dependencies": []
    },
    { 
        "package": { 
            "key": "django",
            "package_name": "Django",
            "installed_version": "2.2.12"
        },
        "dependencies": [
            { 
                "key": "sqlparse",
                "package_name": "sqlparse",
                "installed_version": "0.3.1",
                "required_version": null
            },
            { 
                "key": "pytz",
                "package_name": "pytz",
                "installed_version": "2019.3",
                "required_version": null
            }
        ]
    }
]

使用 Json 格式只显示顶层依赖树,不包括被依赖包的依赖树

(temp-test) [adam@ZCY ~]$ pipdeptree --json-tree

[
    { 
        "key": "django",
        "package_name": "Django",
        "installed_version": "2.2.12",
        "required_version": "2.2.12",
        "dependencies": [
            { 
                "key": "pytz",
                "package_name": "pytz",
                "installed_version": "2019.3",
                "required_version": "Any",
                "dependencies": []
            },
            { 
                "key": "sqlparse",
                "package_name": "sqlparse",
                "installed_version": "0.3.1",
                "required_version": "Any",
                "dependencies": []
            }
        ]
    },
    { 
        "key": "pipdeptree",
        "package_name": "pipdeptree",
        "installed_version": "0.13.2",
        "required_version": "0.13.2",
        "dependencies": [
            { 
                "key": "pip",
                "package_name": "pip",
                "installed_version": "20.0.2",
                "required_version": ">=6.0.0",
                "dependencies": []
            }
        ]
    },
    { 
        "key": "setuptools",
        "package_name": "setuptools",
        "installed_version": "45.2.0",
        "required_version": "45.2.0",
        "dependencies": []
    },
    { 
        "key": "wheel",
        "package_name": "wheel",
        "installed_version": "0.34.2",
        "required_version": "0.34.2",
        "dependencies": []
    }
]

使用“帮助”获取更多详细信息

(temp-test) [adam@ZCY ~]$ pipdeptree -h

usage: pipdeptree [-h] [-v] [-f] [-a] [-l] [-u] [-w [{ silence,suppress,fail}]]
                  [-r] [-p PACKAGES] [-e PACKAGES] [-j] [--json-tree]
                  [--graph-output OUTPUT_FORMAT]

Dependency tree of the installed python packages

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         show program's version number and exit
  -f, --freeze          Print names so as to write freeze files
  -a, --all             list all deps at top level
  -l, --local-only      If in a virtualenv that has global access do not show
                        globally installed packages
  -u, --user-only       Only show installations in the user site dir
  -w [{ silence,suppress,fail}], --warn [{ silence,suppress,fail}]
                        Warning control. "suppress" will show warnings but
                        return 0 whether or not they are present. "silence"
                        will not show warnings at all and always return 0.
                        "fail" will show warnings and return 1 if any are
                        present. The default is "suppress".
  -r, --reverse         Shows the dependency tree in the reverse fashion ie.
                        the sub-dependencies are listed with the list of
                        packages that need them under them.
  -p PACKAGES, --packages PACKAGES
                        Comma separated list of select packages to show in the
                        output. If set, --all will be ignored.
  -e PACKAGES, --exclude PACKAGES
                        Comma separated list of select packages to exclude
                        from the output. If set, --all will be ignored.
  -j, --json            Display dependency tree as json. This will yield "raw"
                        output that may be used by external tools. This option
                        overrides all other options.
  --json-tree           Display dependency tree as json which is nested the
                        same way as the plain text output printed by default.
                        This option overrides all other options (except
                        --json).
  --graph-output OUTPUT_FORMAT
                        Print a dependency graph in the specified output
                        format. Available are all formats supported by
                        GraphViz, e.g.: dot, jpeg, pdf, png, svg
    原文作者:赵成永
    原文地址: https://blog.csdn.net/Adam309050449/article/details/105733762
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞