새소식

ML Development - 2022.06.21

[Python] pipreqes으로 사용한 패키지만 requirements.txt에 포함하기

  • -

pip freeze의 문제점


패키지 관리를 위해 일반적으로 pip freeze > requirements.txt를 사용한다.

pip freeze는 편리하지만, 의존 패키지까지 모두 포함 시켜 가독성을 떨어트린다는 문제가 있다.

 

실제로 aiohttp, beautifulsoup4, GitPython, requests만 사용한 경우에도, requirements.txt는 다음과 같이 굉장히 지저분하게 생성된다.

pip freeze > requirements.txt
>>
aiohttp==3.8.4
aiosignal==1.3.1
async-timeout==4.0.2
asyncio==3.4.3
attrs==22.2.0
autopep8==2.0.2
beautifulsoup4==4.11.2
certifi==2022.12.7
charset-normalizer==3.1.0
docopt==0.6.2
frozenlist==1.3.3
gitdb==4.0.10
GitPython==3.1.31
idna==3.4
multidict==6.0.4
pipreqs==0.4.11
pycodestyle==2.10.0
requests==2.28.2
smmap==5.0.0
soupsieve==2.4
tomli==2.0.1
urllib3==1.26.14
yarg==0.1.9
yarl==1.8.2

 

pipreqs으로 문제 해결하기


pipreqs를 사용하면 pip install에 포함돼야 하는 핵심적인 패키지만 requirements.txt에 저장할 수 있다.

사용법도 굉장히 쉬운 편이니 설치해서 활용해보자.

 

설치 하기
pip install pipreqs

 

사용 하기
  • 확인할 프로젝트 경로만 지정해주면 된다.
pipfreqs /project_path
  • 앞서 pip freeze와 동일한 경로에서 pipreqs를 실행하면, 다음과 같이 requirements.txt가 깔끔하게 작성된다.
pipreqs ./
>>
aiohttp==3.8.4
beautifulsoup4==4.11.2
GitPython==3.1.31
requests==2.28.2

 

pipreqs 오류 해결하기


Unicode 관련 오류
  • 윈도우 환경에서 발생하는 오류라고 한다.
pipreqs ./
>>
UnicodeDecodeError: 'cp949' codec can't decode byte 0xe2 in position 6067: illegal multibyte sequence
  • 명령어에  --encoding=utf-8-sig를 적어주면 해결된다.
$ pipreqs ./ --encoding=utf-8-sig
 

Pipreqs: UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 1206: character maps to <undefined>

When I use pipreqs, I have this problem. I use anaconda and Russian Windows. root@DESKTOP-ETLLRI1 C:\Users\root\Desktop\resumes $ pipreqs C:\Users\root\Desktop\resumes Traceback (most recent call l...

stackoverflow.com

 

SyntaxError 관련 오류
  • 프로젝트 폴더 안에 가상 환경 .venv가 있을 경우 발생했다.
  • --ignore .venv으로 가상 환경을 제외하도록 설정하면 해결 된다.
pipreqs ./ --ignore .venv --encoding=utf-8-sig
 

Pipreqs requirements.txt is not correct

Hello I am having troubles with the pipreqs librairy in Python. It doesn't generate the correct requirements.txt file. I am using a Python Virtual Environment and the only packages I have installed...

stackoverflow.com

 

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.