-
requirements.txt
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 :
-
Requirements files
are used to hold the result frompip freeze
for the purpose of achieving repeatable installations. In this case, your requirement file contains a pinned version of everything that was installed whenpip freeze
was run.pip freeze > requirements.txt pip install -r requirements.txt
-
Requirements files are used to force pip to properly resolve dependencies.
-
Requirements files are used to force pip to install an alternate version of a subdependency
-
Requirements files are used to override a dependency with a local patch that lives in version control.
-
-
Pipfile
Pipfile
(GitHub) and its sisterPipfile.lock
are a replacement for the existing standard pip’srequirements.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 otherlibraries
andapplications
(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 viainstall_requires
insetup.py
. -
Applications
Libraries
are ultimately meant to be used in someapplication
. 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 thePipfile
and use this file to update the set of concrete dependencies inPipfile.lock
. This file defines a specific idempotent environment that is known to work for your project. Pipfile
andPipenv
can be used to define a development or test environment.
- For
-
References
理解Pipfile vs. setup.py || applications vs. libraries
最新推荐文章于 2023-02-17 16:09:42 发布