欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

理解Pipfile vs. setup.py || applications vs. libraries

程序员文章站 2022-05-07 15:13:08
...
  • requirements.txt

    requirements file format

    requirement specifiers

    PEP 508 – Dependency specification for Python Software Packages

    Requirements files are files containing a list of items to be installled using pip install like so :

    pip install -r requirements.txt

    Logically, a Requirements file is just a list of pip install arguments placed in a file.

    There are 4 common uses of Requirements files :

    1. Requirements files are used to hold the result from pip freeze for the purpose of achieving repeatable installations. In this case, your requirement file contains a pinned version of everything that was installed when pip freeze was run.

      pip freeze > requirements.txt
      pip install -r requirements.txt
      
    2. Requirements files are used to force pip to properly resolve dependencies.

    3. Requirements files are used to force pip to install an alternate version of a subdependency

    4. Requirements files are used to override a dependency with a local patch that lives in version control.

  • Pipfile

    Pipfile(GitHub) and its sister Pipfile.lock are a replacement for the existing standard pip’s requirements.txt file.

    pip install -r requirements.txt
    pip install -p Pipfile # 安装
    pip freeze -p Pipfile # 生成
    
  • distinction between applications and libraries

  • Libraries

    Libraries provide reusable functionality to other libraries and applications (let’s use the umbrella term project here). They are required to work alongside other libraries, all with their own set of sub-dependencies. They defines abstract dependencies. To avoid version conflicts in sub-dependencies of different libraries within a project, libraries shouyld never ever pin dependency versions. Although they may specify lower or (less frequently) upper bounds, if they rely on some specific feature/fix/bug. Library dependencies are specified via install_requires in setup.py.

  • Applications

    Libraries are ultimately meant to be used in some application. Applications are different in that they usually are not depended on by other projects. They are meant to be deployed into some specific environment and only then should the exact versions of all their dependencies and sub-dependencies be made concrete.

  • To summarize
    • For libraries, define abstract dependencies via install_requires in setup.py. The decision of which version exactly to be installed and where to obtain that dependency is not yours to make.
    • For applications, define dependencies and where to get them in the Pipfile and use this file to update the set of concrete dependencies in Pipfile.lock. This file defines a specific idempotent environment that is known to work for your project.
    • Pipfile and Pipenv can be used to define a development or test environment.
  • References

  1. Pipfile vs setup.py
  2. pip 与 Pipfile
  3. Ken Reitz : Announcing Pipenv
  4. How are Pipfile and Pipfile.lock used?