tell pip to install the dependencies of packages listed in a requirement file

前端 未结 5 1653
耶瑟儿~
耶瑟儿~ 2020-12-12 19:10

Developing a Django web app, I have a list of packages I need to install in a virtualenv. Say:

Django==1.3.1
--extra-index-url=http://dist.pinaxproject.com/         


        
相关标签:
5条回答
  • 2020-12-12 19:27

    Extending Piotr's answer, if you also need a way to figure what to put in requirements.in, you can first use pip-chill to find the minimal set of required packages you have. By combining these tools, you can show the dependency reason why each package is installed. The full cycle looks like this:

    1. Create virtual environment:
      $ python3 -m venv venv
    2. Activate it:
      $ . venv/bin/activate
    3. Install newest version of pip, pip-tools and pip-chill:
      (venv)$ pip install --upgrade pip
      (venv)$ pip install pip-tools pip-chill
    4. Build your project, install more pip packages, etc, until you want to save...
    5. Extract minimal set of packages (ie, top-level without dependencies):
      (venv)$ pip-chill --no-version > requirements.in
    6. Compile list of all required packages (showing dependency reasons):
      (venv)$ pip-compile requirements.in
    7. Make sure the current installation is synchronized with the list:
      (venv)$ pip-sync
    0 讨论(0)
  • 2020-12-12 19:33

    Here's a simple solution:

    while read -r package; do pip install --upgrade --force-reinstall $package;done < pipfreeze.txt
    
    0 讨论(0)
  • 2020-12-12 19:37

    Given your comment to the question (where you say that executing the install for a single package works as expected), I would suggest looping over your requirement file. In bash:

    #!/bin/sh
    while read p; do
      pip install $p
    done < requirements.pip
    

    HTH!

    0 讨论(0)
  • 2020-12-12 19:41

    simplifily, use:

    pip install -r requirement.txt
    

    it can install all listed in requirement file.

    0 讨论(0)
  • 2020-12-12 19:44

    Any way to do this without manually re-installing the packages in a new virtualenv to get their dependencies ? This would be error-prone and I'd like to automate the process of cleaning the virtualenv from no-longer-needed old dependencies.

    That's what pip-tools package is for (from https://github.com/jazzband/pip-tools):

    Installation

    $ pip install --upgrade pip  # pip-tools needs pip==6.1 or higher (!)
    $ pip install pip-tools
    

    Example usage for pip-compile

    Suppose you have a Flask project, and want to pin it for production. Write the following line to a file:

    # requirements.in
    Flask
    

    Now, run pip-compile requirements.in:

    $ pip-compile requirements.in
    #
    # This file is autogenerated by pip-compile
    # Make changes in requirements.in, then run this to update:
    #
    #    pip-compile requirements.in
    #
    flask==0.10.1
    itsdangerous==0.24        # via flask
    jinja2==2.7.3             # via flask
    markupsafe==0.23          # via jinja2
    werkzeug==0.10.4          # via flask
    

    And it will produce your requirements.txt, with all the Flask dependencies (and all underlying dependencies) pinned. Put this file under version control as well and periodically re-run pip-compile to update the packages.

    Example usage for pip-sync

    Now that you have a requirements.txt, you can use pip-sync to update your virtual env to reflect exactly what's in there. Note: this will install/upgrade/uninstall everything necessary to match the requirements.txt contents.

    $ pip-sync
    Uninstalling flake8-2.4.1:
      Successfully uninstalled flake8-2.4.1
    Collecting click==4.1
      Downloading click-4.1-py2.py3-none-any.whl (62kB)
        100% |████████████████████████████████| 65kB 1.8MB/s
      Found existing installation: click 4.0
        Uninstalling click-4.0:
          Successfully uninstalled click-4.0
    Successfully installed click-4.1
    
    0 讨论(0)
提交回复
热议问题