개요

pip freeze 명령으로 현재 프로젝트에서 사용하고 있는 의존성 모듈과 버전을 출력할 수 있습니다.

$ pip freeze

argcomplete==1.12.3
beautifulsoup4==4.6.3
certifi==2021.10.8
cffi==1.14.4
chardet==3.0.4
BASH

하지만 알파벳 순서로만 출력하고 의존성 관계는 표시하지 않는 단점이 있습니다.

pipdeptree 는 이런 단점을 해결하는 파이썬 패키지의 dependency tree 를 그려주는 패키지로 pip 로 설치하면 됩니다.

$ pip install pipdeptree
BASH

사용

pipdeptree 를 실행하면  의존성을 들여쓰기를 통해서 표시하므로 pip freeze 보다 가독성 좋게 읽을 수 있습니다.

의존성 출력

$ pipdeptree

arrow==1.2.0
  - python-dateutil [required: >=2.7.0, installed: 2.8.2]
    - six [required: >=1.5, installed: 1.16.0]
colorama==0.4.4
cryptography==35.0.0
  - cffi [required: >=1.12, installed: 1.14.6]
    - pycparser [required: Any, installed: 2.20]
Faker==9.3.1
  - python-dateutil [required: >=2.4, installed: 2.8.2]
    - six [required: >=1.5, installed: 1.16.0]
  - text-unidecode [required: ==1.3, installed: 1.3]
faker-airtravel==0.4
jupyterlab==3.1.18
  - ipython [required: Any, installed: 7.28.0]
    - backcall [required: Any, installed: 0.2.0]
    - decorator [required: Any, installed: 5.1.0]
    - jedi [required: >=0.16, installed: 0.18.0]
      - parso [required: >=0.8.0,<0.9.0, installed: 0.8.2]
    - matplotlib-inline [required: Any, installed: 0.1.3]
      - traitlets [required: Any, installed: 5.1.0]
BASH

패키지 기반 의존성 출력

현재 프로젝트의 모든 의존성을 출력하지만 반대로 특정 패키지에 의존하는 경로가 궁금할 수 있습니다.

이런 경우 -r, --reverse 옵션에 -p, --packages 로 패키지 이름을 지정해 주면 됩니다.

다음은 tornado 와 xlrd 에 의존하는 패키지 목록을 표시합니다.

$ pipdeptree -r -p tornado,xlrd
BASH


json 출력

pipdeptree 는 -j 옵션을 주면 json 으로도 출력이 가능합니다.

$ pipdeptree -j

[
    {
        "package": {
            "key": "anyio",
            "package_name": "anyio",
            "installed_version": "3.3.3"
        },
        "dependencies": [
            {
                "key": "idna",
                "package_name": "idna",
                "installed_version": "3.2",
                "required_version": ">=2.8"
            },
BASH

graphviz 출력

그래픽 도구인 GraphViz 를 설치했다면 PDF 나 PNG, SVG 로도 출력이 가능합니다.

먼저 graphviz의 python 용 library 를 설치합니다.

$ pip install graphviz
BASH


다른 포맷으로 출력하려면 --graph-output 옵션 뒤에 출력하려면 포맷(pdf, jpeg, png, svg)을 지정하며 다음은 pdf 로 출력합니다.

$ pipdeptree --graph-output pdf > dep.pdf
$ pipdeptree --graph-output png > dep.png
BASH

png 출력

문제 해결

graphviz is not available

graphviz 를 설치했는데도 아래 에러가 난다면 phthon 용 graphviz 를 설치하지 않은 것입니다.

graphviz is not available, but necessary for the output option. Please install it.
BASH


pip 로 pgraphviz 모듈을  설치해 주면 됩니다.

$ pip install graphviz
BASH

no attribute 'FORMATS'

다음과 같이 "AttributeError: module 'graphviz.backend' has no attribute 'FORMATS' 에러가 나는 경우가 있습니다.

  File "C:\Users\lesstif\github\python\python-package-usage\venv\lib\site-packages\pipdeptree.py", line 877, in main
    output = dump_graphviz(tree,
  File "C:\Users\lesstif\github\python\python-package-usage\venv\lib\site-packages\pipdeptree.py", line 601, in dump_graphviz
    if output_format not in backend.FORMATS:
AttributeError: module 'graphviz.backend' has no attribute 'FORMATS'
CODE

아마 graphviz 모듈과 pipdeptree 의 의존성에 문제가 있어서 없는 attribute 를 참고해서 발생하는 것 같고 저는 pipdeptree.py 의 문제되는 라인을 주석 처리해서 해결했습니다..

 if output_format not in backend.FORMATS:  ## 여기부터 모두 주석 처리
        print('{0} is not a supported output format.'.format(output_format),
              file=sys.stderr)
        print('Supported formats are: {0}'.format(
            ', '.join(sorted(backend.FORMATS))), file=sys.stderr)
        sys.exit(1)
PY


같이 보기

Ref