查看已安装的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
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: 要查看已经在Linux系统上安装Python,可以使用以下命令: ``` pip list ``` 这将列出所有已经安装Python和它们的版本号。 如果你使用Anaconda可以使用: ``` conda list ``` 这将列出所有已经安装Python和它们的版本号。 ### 回答2: 在Linux中,可以使用以下命令查看Python安装: 1. 列出所有已安装Python: ``` pip list ``` 2. 查看特定Python的详细信息: ``` pip show <package_name> ``` 其中,`<package_name>`为要查看的名称。该命令将显示有关的详细信息,例如版本号、作者、描述等。 3. 查看Python安装路径: ``` pip show -f <package_name> ``` 该命令将显示安装路径及其相关文件。 4. 查看Python依赖关系: ``` pip show --requires <package_name> ``` 该命令将列出的所有依赖项。 5. 利用tree命令显示Python及其依赖关系的状结构: ``` pipdeptree ``` 若未安装`pipdeptree`,可以使用以下命令安装: ``` pip install pipdeptree ``` `pipdeptree`命令将输出一个类似的结构,显示每个及其依赖关系。 以上命令可以帮助你在Linux中查看Python安装及其相关信息。 ### 回答3: 在Linux上,我们可以使用`pip`命令来查看Python安装。 1. 首先,打开终端窗口。 2. 输入`pip list`命令,并按下回车键。 3. 终端会列出所有已安装Python的名称和版本号。 例如: ``` Package Version ------------------ ------- numpy 1.19.5 pandas 1.2.0 matplotlib 3.3.4 ... ``` 对于每个,第一列是的名称,第二列是的版本号。 4. 如果想获取更详细的信息以及安装路径等信息,可以使用`pip show 名`命令。 例如:`pip show numpy`会显示关于numpy的详细信息。 ``` Name: numpy Version: 1.19.5 Summary: NumPy is the fundamental package for array computing with Python. Home-page: https://www.numpy.org ... ``` 这将列出的名称、版本号、摘要、主页等信息。 5. 如果想查找特定的,可以使用`pip search 名`命令。 例如:`pip search numpy`会显示所有与"numpy"相关的的信息。 ``` numpy (1.19.5) - NumPy is the fundamental package for array computing with Python. numpy-f2py (1.20.0) - Generate and compile Fortran 77 or Fortran 90 code ... ``` 这将列出所有含关键字"numpy"的的名称和摘要。 通过使用以上命令,您可以方便地查看安装Python及其详细信息。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值