본문 바로가기
IT/Python

파이썬 설치된 패키지의 의존성을 트리로 확인해보자

by 소소메모 2020. 10. 13.
반응형

파이썬 프로젝트를 하다 보면, 설치된 패키지의 라이선스나 취약점 등을 파악하기 위해

혹은 버전 충돌로 설치가 잘 안되거나 오류 가 있을 경우 등의 상황에서

설치된 패키지의 의존관계를 확인할 경우가 있습니다.

 

이 때 도움이 될 수 있는 설치된 파이썬 패키지의 의존관계를 트리 형태로 출력하는

pipdeptree 가 있습니다.

 

설치 및 사용법은 아주 간단합니다.

 

1. 설치

> pip install pipdeptree

 

2. 실행

> pipdeptree
Warning!!! Possible conflicting dependencies found:
* Mako==0.9.1 -> MarkupSafe [required: >=0.9.2, installed: 0.18]
  Jinja2==2.7.2 -> MarkupSafe [installed: 0.18]
------------------------------------------------------------------------
wordcloud==1.6.0
  - matplotlib [required: Any, installed: 3.2.0]
    - cycler [required: >=0.10, installed: 0.10.0]
      - six [required: Any, installed: 1.14.0]
    - kiwisolver [required: >=1.0.1, installed: 1.1.0]
      - setuptools [required: Any, installed: 41.2.0]
    - numpy [required: >=1.11, installed: 1.18.1]
    - pyparsing [required: >=2.0.1,!=2.1.6,!=2.1.2,!=2.0.4, installed: 2.4.6]
    - python-dateutil [required: >=2.1, installed: 2.8.1]
      - six [required: >=1.5, installed: 1.14.0]
  - numpy [required: >=1.6.1, installed: 1.18.1]
  - pillow [required: Any, installed: 7.0.0]
image==1.5.28
  - django [required: Any, installed: 3.0.4]
    - asgiref [required: ~=3.2, installed: 3.2.7]
    - pytz [required: Any, installed: 2019.3]
    - sqlparse [required: >=0.2.2, installed: 0.3.1]
  - pillow [required: Any, installed: 7.0.0]
xlrd==1.2.0

 

 

3. JSON 파일로 출력 및 저장할 수 있고,

> pipdeptree --json > dependencies.json

 

 

4. 의존관계 트리 이미지를 생성하여 pdf, png 등으로 저장할 수도 있습니다.

단, Graphviz 가 설치되어 있어야 합니다. (설치 링크 : www.graphviz.org/download/ )

> pipdeptree --graph-output png > dependencies.png

 

 

dependency tree

 

상세 사용법은 다음과 같습니다.

 

> pipdeptree --help
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

 

도움이 되셨기를 바랍니다.

반응형

댓글