공공연히 개발하기 🧑‍💻/Python

[Python] 명령어 하나로 requirements.txt 파일 손쉽게 생성 및 설치하기

공공연히 2024. 12. 14. 21:10

 

파이썬에 설치된 라이브러리들을 다른 환경에 적용할 때 활용하는 requirements.txt 파일
호옥시나 일일이 옮겨적고 있었다면?

명령어 하나로 현재 파이썬에 설치된 패키지 리스트를 바로 requirements.txt 파일로 만들어 보자.

1. freeze 로 간단하게 생성

pip freeze > requirements.txt

를 수행하면 아래 내용처럼 requirements.txt 파일이 생성된다.

aiohappyeyeballs==2.4.4
aiohttp==3.11.10
aiosignal==1.3.1
annotated-types==0.7.0
anyio==4.7.0
asgiref==3.8.1
attrs==24.2.0
certifi==2024.8.30
charset-normalizer==3.4.0
distro==1.9.0
Django==5.1.3
djangorestframework==3.15.2
frozenlist==1.5.0
h11==0.14.0
httpcore==1.0.7
httpx==0.28.1
idna==3.10
jiter==0.8.0
multidict==6.1.0
openai==0.28.0
propcache==0.2.1
pydantic==2.10.3
pydantic_core==2.27.1
requests==2.32.3
sniffio==1.3.1
sqlparse==0.5.2
tqdm==4.67.1
typing_extensions==4.12.2
urllib3==2.2.3
yarl==1.18.3

⭐ 여기서 주의할 점은 pip와 setuptools 패키지가 없다는 것!

해당 패키지들도 포함시키고 싶다면 아래와 같이 하면 된다.

 

2. pip list, --format 옵션 사용하기

pip list --format=freeze > requirements.txt

pip list 자체를 requirements.txt로 만드는 방법이다.
format 옵션을 사용하여 내가 원하는 형식으로 requirements.txt 파일을 생성할 수 있다.
--format : freeze, json, columns


▶️ freeze

pip list --format=freeze > requirements.txt

requirements.txt의 내용

aiohappyeyeballs==2.4.4
aiohttp==3.11.10
aiosignal==1.3.1
annotated-types==0.7.0
anyio==4.7.0
asgiref==3.8.1
attrs==24.2.0
certifi==2024.8.30
charset-normalizer==3.4.0
distro==1.9.0
Django==5.1.3
djangorestframework==3.15.2
frozenlist==1.5.0
h11==0.14.0
httpcore==1.0.7
httpx==0.28.1
idna==3.10
jiter==0.8.0
multidict==6.1.0
openai==0.28.0
pip==24.3.1
propcache==0.2.1
pydantic==2.10.3
pydantic_core==2.27.1
requests==2.32.3
setuptools==65.5.0
sniffio==1.3.1
sqlparse==0.5.2
tqdm==4.67.1
typing_extensions==4.12.2
urllib3==2.2.3
yarl==1.18.3

-> pip와 setuptools 패키지가 포함된 것을 확인할 수 있다.

 

▶️ json

pip3 list --format=json > requirements.txt

requirements.txt의 내용

[{"name": "aiohappyeyeballs", "version": "2.4.4"}, {"name": "aiohttp", "version": "3.11.10"}, {"name": "aiosignal", "version": "1.3.1"}, {"name": "annotated-types", "version": "0.7.0"}, {"name": "anyio", "version": "4.7.0"}, {"name": "asgiref", "version": "3.8.1"}, {"name": "attrs", "version": "24.2.0"}, {"name": "certifi", "version": "2024.8.30"}, {"name": "charset-normalizer", "version": "3.4.0"}, {"name": "distro", "version": "1.9.0"}, {"name": "Django", "version": "5.1.3"}, {"name": "djangorestframework", "version": "3.15.2"}, {"name": "frozenlist", "version": "1.5.0"}, {"name": "h11", "version": "0.14.0"}, {"name": "httpcore", "version": "1.0.7"}, {"name": "httpx", "version": "0.28.1"}, {"name": "idna", "version": "3.10"}, {"name": "jiter", "version": "0.8.0"}, {"name": "multidict", "version": "6.1.0"}, {"name": "openai", "version": "0.28.0"}, {"name": "pip", "version": "24.3.1"}, {"name": "propcache", "version": "0.2.1"}, {"name": "pydantic", "version": "2.10.3"}, {"name": "pydantic_core", "version": "2.27.1"}, {"name": "requests", "version": "2.32.3"}, {"name": "setuptools", "version": "65.5.0"}, {"name": "sniffio", "version": "1.3.1"}, {"name": "sqlparse", "version": "0.5.2"}, {"name": "tqdm", "version": "4.67.1"}, {"name": "typing_extensions", "version": "4.12.2"}, {"name": "urllib3", "version": "2.2.3"}, {"name": "yarl", "version": "1.18.3"}]

 

▶️ columns

pip3 list --format=columns > requirements.txt

requirements.txt의 내용

Package             Version
------------------- ---------
aiohappyeyeballs    2.4.4
aiohttp             3.11.10
aiosignal           1.3.1
annotated-types     0.7.0
anyio               4.7.0
asgiref             3.8.1
attrs               24.2.0
certifi             2024.8.30
charset-normalizer  3.4.0
distro              1.9.0
Django              5.1.3
djangorestframework 3.15.2
frozenlist          1.5.0
h11                 0.14.0
httpcore            1.0.7
httpx               0.28.1
idna                3.10
jiter               0.8.0
multidict           6.1.0
openai              0.28.0
pip                 24.3.1
propcache           0.2.1
pydantic            2.10.3
pydantic_core       2.27.1
requests            2.32.3
setuptools          65.5.0
sniffio             1.3.1
sqlparse            0.5.2
tqdm                4.67.1
typing_extensions   4.12.2
urllib3             2.2.3
yarl                1.18.3

 

3. requirements.txt 파일로 설치하기

pip install -r requirements.txt

 

 

▶️ --no-cache 옵션으로 캐시를 사용하지 않고 설치

pip install --no-cache -r requirements.txt

 

▶️ --upgrade 로 최신 버전 설치

requirements.txt 파일에 버전이 명시되어 있지 않은 경우에도 최신 버전 설치가 가능하다.

pip install --no-cache --upgrade -r requirements.txt