programing

pip 요구 사항 파일을 사용하여 패키지를 제거하고 설치하려면 어떻게해야합니까?

nasanasas 2020. 11. 4. 08:03
반응형

pip 요구 사항 파일을 사용하여 패키지를 제거하고 설치하려면 어떻게해야합니까?


개발 중에 변경되는 pip 요구 사항 파일이 있습니다.

요구 사항 파일에 나타나지 않는 패키지 pip제거 하고 나타나는 패키지 설치 하도록 만들 수 있습니까 ? 표준 방법이 있습니까?

이렇게하면 pip 요구 사항 파일이 패키지의 정식 목록 ( 'if and only if'접근 방식)이 될 수 있습니다.

업데이트 : https://github.com/pypa/pip/issues/716 에서 새로운 기능으로 제안했습니다.


짧은 대답은 아니오입니다. pip로는 그렇게 할 수 없습니다.


requirements.txt에없는 것은 제거해야합니다.

pip freeze | grep -v -f requirements.txt - | grep -v '^#' | xargs pip uninstall -y

-e방법은와 함께 설치된 패키지 , 즉 git 저장소 또는 이와 유사한 패키지에서는 제대로 작동하지 않습니다 . 이를 건너 뛰려면 -e플래그로 시작하는 패키지를 필터링하면됩니다 .

pip freeze | grep -v -f requirements.txt - | grep -v '^#' | grep -v '^-e ' | xargs pip uninstall -y

그런 다음 분명히 :

pip install -r requirements.txt

2016 년 업데이트 : 위의 접근 방식을 실제로 사용하고 싶지는 않을 것입니다. 확인 pip-tools하고pip-sync 있는 당신은 아마 훨씬 더 강력한 방법으로 할 무엇을 찾고있는 달성.

https://github.com/nvie/pip-tools

2016 년 5 월 업데이트 :

이제를 사용할 수도 pip uninstall -r requirements.txt있지만 기본적으로 반대의 작업을 수행합니다.requirements.txt

2019 년 5 월 업데이트 :

pipenv를 확인하십시오 . 패키지 관리의 세계에서 이런 종류의 질문을 쓸모 없게 만드는 많은 일이 발생했습니다.


의 기능이 pip아닙니다. 당신이 정말로 그런 일을하려는 경우의 출력을 비교하는 스크립트를 작성할 수 있습니다 pip freeze당신과를 requirements.txt하지만 가능성이 그것의 가치보다 더 많은 번거 로움이 될 것입니다.

를 사용하면 다음 virtualenv과 같이 깨끗한 환경을 만들고에서 (재) 설치하는 것이 더 쉽고 안정적입니다 requirements.txt.

deactivate
rm -rf venv/
virtualenv venv/
source venv/bin/activate
pip install -r requirements.txt

이제 -r requirements.txt인수를에 전달할 수 있습니다 pip uninstall.

pip uninstall -r requirements.txt -y

적어도 pip8.1.2부터 다음을 pip help uninstall표시합니다.

...
Uninstall Options:
  -r, --requirement <file>    Uninstall all the packages listed in the given requirements file.  This option can be
                              used multiple times.
...

Stephen의 제안은 좋은 생각이지만 파일에 직접적인 요구 사항 만 포함하면 작동하지 않습니다.

를 포함하여 모든 종속성이 제거 distribute되어 pip자체적으로 분해 됩니다.

가상 환경을 추적하는 동안 깨끗한 요구 사항 파일 유지

가상 환경을 버전 추적하는 방법은 다음과 같습니다. requirements.txt직접 요구 사항 만 포함 하여 최소한의을 유지하려고 노력하고 확실하지 않은 버전 제약은 언급하지 않습니다.

그러나 그 외에도 venv.pip파일 에있는 내 virtualenv의 실제 상태 인 버전 추적 (예 : git)을 유지하고 포함 합니다.

다음은 샘플 워크 플로입니다.


버전 추적을 사용하여 virtualenv 작업 공간을 설정하십시오.

mkdir /tmp/pip_uninstalling
cd /tmp/pip_uninstalling
virtualenv venv
. venv/bin/activate

버전 추적 시스템 초기화 :

git init
echo venv > .gitignore
pip freeze > venv.pip
git add .gitignore venv.pip
git commit -m "Python project with venv"

종속성이있는 패키지를 설치하고 요구 사항 파일에 포함합니다.

echo flask > requirements.txt
pip install -r requirements.txt
pip freeze > venv.pip

이제 앱 빌드를 시작한 다음 새 브랜치를 커밋하고 시작합니다.

vim myapp.py
git commit -am "Simple flask application"
git checkout -b "experiments"

추가 패키지 설치 :

echo flask-script >> requirements.txt
pip install -r requirements.txt
pip freeze > venv.pip

... 가지고 놀다가 이전 버전으로 돌아 오세요

vim manage.py
git commit -am "Playing with flask-script"
git checkout master

이제 불필요한 패키지를 제거하십시오.

pip freeze | grep -v -f venv.pip | xargs pip uninstall -y

프로세스가 git hooks로 자동화 될 수 있다고 생각하지만 주제에서 벗어나지 말자.

Of course, it makes sense then to use some package caching system or local repository like pip2pi


Piggybacking off @stephen-j-fuhry here is a powershell equivalent I use:

pip freeze | ? { $_ -notmatch ((gc req.txt) -join "|") }

This is an old question (but a good one), and things have changed substantially since it was asked.

There's an offhand reference to pip-sync in another answer, but deserves its own answer, because it solves precisely the OP's problem.

pip-sync takes a requirements.txt file as input, and "trues up" your current Python environment so that it matches exactly what's in that requirements.txt. This includes removing any packages that are present in your env but absent from requirements.txt.

Example: Suppose we want our env to contain (only) 3 libraries: libA, libB, and libC, like so:

> cat requirements.txt
libA==1.0
libB==1.1
libC==1.2

But our env currently contains libC and libD:

> pip freeze
libC==1.2
libD==1.3

Running pip-sync will result in this, which was our desired final state:

> pip-sync requirements.txt
> pip freeze
libA==1.0
libB==1.1
libC==1.2

While this doesn't directly answer the question, a better alternative to requirements.txt now is using a Pipfile. This functions similarly to a Ruby Gemfile. Currently, you need to make use of the pipenv tool but hopefully this will eventually be incorporated into pip. This provides the pipenv clean command which does what you want.

(Note that you can import an existing requirements.txt with pipenv install -r requirements.txt. After this you should have a Pipfile and the requirements.txt can be removed.)


It is possible now using:

pip uninstall -r requirements.txt

참고URL : https://stackoverflow.com/questions/13176968/how-can-i-use-a-pip-requirements-file-to-uninstall-as-well-as-install-packages

반응형