How can I use a pip requirements file to uninstall as well as install packages?

前端 未结 10 1312
猫巷女王i
猫巷女王i 2021-01-30 04:48

I have a pip requirements file that changes during development.

Can pip be made to uninstall packages that do not appear in the requirement

10条回答
  •  梦如初夏
    2021-01-30 05:17

    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
    

提交回复
热议问题