IT박스

여러 환경에 대한 requirements.txt를 사용자 지정하는 방법은 무엇입니까?

itboxs 2020. 9. 14. 08:12
반응형

여러 환경에 대한 requirements.txt를 사용자 지정하는 방법은 무엇입니까?


저는 개발과 생산이라는 두 가지 지점이 있습니다. 각각에는 종속성이 있으며 일부는 다릅니다. 개발은 자체적으로 개발중인 종속성을 가리 킵니다. 생산도 마찬가지입니다. 'requirements.txt'라는 단일 파일에서 각 분기의 종속성을 예상하는 Heroku에 배포해야합니다.

정리하는 가장 좋은 방법은 무엇입니까?

내가 생각한 것 :

  • 각 분기에 하나씩 별도의 요구 사항 파일을 유지합니다 (빈번한 병합에서 살아남 아야합니다!).
  • 어떤 요구 사항 파일을 사용하고 싶은지 Heroku에게 알려주세요 (환경 변수?)
  • 배포 스크립트 작성 (임시 분기 생성, 요구 사항 파일 수정, 커밋, 배포, 임시 분기 삭제)

요구 사항 파일을 계단식으로 배열하고 "-r"플래그를 사용하여 pip에 한 파일의 내용을 다른 파일 안에 포함하도록 지시 할 수 있습니다. 요구 사항을 다음과 같이 모듈 식 폴더 계층으로 나눌 수 있습니다.

`-- django_project_root
|-- requirements
|   |-- common.txt
|   |-- dev.txt
|   `-- prod.txt
`-- requirements.txt

파일의 내용은 다음과 같습니다.

common.txt :

# Contains requirements common to all environments
req1==1.0
req2==1.0
req3==1.0
...

dev.txt :

# Specifies only dev-specific requirements
# But imports the common ones too
-r common.txt
dev_req==1.0
...

prod.txt :

# Same for prod...
-r common.txt
prod_req==1.0
...

이제 Heroku 외부에서 다음과 같은 환경을 설정할 수 있습니다.

pip install -r requirements/dev.txt

또는

pip install -r requirements/prod.txt

Heroku는 특별히 프로젝트 루트에서 "requirements.txt"를 찾기 때문에 다음과 같이 prod를 미러링해야합니다.

requirements.txt :

# Mirrors prod
-r requirements/prod.txt

원래 질문과 답변이 게시되었을 때 존재하지 않았던 오늘날 실행 가능한 옵션은 종속성을 관리하기 위해 pip 대신 pipenv 를 사용 하는 것입니다.

With pipenv, manually managing two separate requirement files like with pip is no longer necessary, and instead pipenv manages the development and production packages itself via interactions on the command line.

To install a package for use in both production and development:

pipenv install <package>

To install a package for the development environment only:

pipenv install <package> --dev

Via those commands, pipenv stores and manages the environment configuration in two files (Pipfile and Pipfile.lock). Heroku's current Python buildpack natively supports pipenv and will configure itself from Pipfile.lock if it exists instead of requirements.txt.

See the pipenv link for full documentation of the tool.


If your requirement is to be able to switch between environments on the same machine, it may be necessary to create different virtualenv folders for each environment you need to switch to.

python3 -m venv venv_dev
source venv_dev/bin/activate
pip install -r pip/common.txt
pip install -r pip/dev.txt
exit
python3 -m venv venv_prod
source venv_prod/bin/activate
pip install -r pip/common.txt
exit
source venv_dev/bin/activate
# now we are in dev environment so your code editor and build systems will work.

# let's install a new dev package:
# pip install awesome
# pip freeze -r pip/temp.txt
# find that package, put it into pip/dev.txt
# rm pip/temp.txt

# pretty cumbersome, but it works. 

참고URL : https://stackoverflow.com/questions/17803829/how-to-customize-a-requirements-txt-for-multiple-environments

반응형