본문 바로가기
Development/Python

python package를 requirements 텍스트로 저장

by shuka 2023. 11. 22.
728x90
728x90

python package를 requirements 텍스트로 저장하는 방법

프로젝트 작업을 하다 보면 가상 환경을 새로 만들거나 다른 PC로 프로젝트를 옮겨야 할 경우가 발생하기도 하고,

 

갑자기 필요한 python library가 생겨서 설치를 하면서 requirements.txt에 작성하는 것을 깜빡할 때도 있다.

 

Docker를 사용한다면 해당 container를 image로 만들고 tar파일로 만들어서 python library를 신경쓸 필요가 없는데

 

아닐 경우 약간 귀찮고 복잡해질 수도 있다.

 

 

 

 

방법1. pip freeze

pip freeze는 현재 환경에서 설치된 python library와 version에 대해 밑의 사진과 같이 출력되는 것을 볼 수 있다.

 

pip freeze

 

 

 

출력된 부분을 복사해서 requirements.txt에 붙여넣기로 해도 되고 다음과 같은 명령어로 해서 바로 txt를 생성할 수 있다.

 

pip freeze > requirements.txt

 

 

다만, 위의 requirements.txt는 프로젝트에 사용되는 library만 있는 것이 아니라서 내가 사용하는 방법은 아니다.

 

 

 

반응형

 

 

방법 2. pipreqs

https://pypi.org/project/pipreqs/

 

pipreqs

Pip requirements.txt generator based on imports in project

pypi.org

 

 

pipreqs는 프로젝트에 사용되는 library에 대해서만 뽑아서 requirements.txt로 생성해준다.

 

pip install pipreqs
pipreqs {project path}

ex) pipreqs ./projects/

 

 

위와 같이 pip install로 pipreqs를 설치하고 pipreqs로 원하는 project path를 argument로 주면 다음과 같이 출력되면서 requirements.txt가 생성된다.

 

WARNING: Import named "torch_tensorrt" not found locally. Trying to resolve it at the PyPI server.
WARNING: Import named "torch_tensorrt" was resolved to "torch-tensorrt:1.4.0" package (https://pypi.org/project/torch-tensorrt/).
Please, verify manually the final list of requirements.txt to avoid possible dependency confusions.
INFO: Successfully saved requirements file in ./requirements.txt

 

728x90
반응형

댓글