依赖的导出和安装requirements.txt

Posted lzyang121

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了依赖的导出和安装requirements.txt相关的知识,希望对你有一定的参考价值。

Requirements Files

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

pip install -r requirements.txt

Details on the format of the files are here: Requirements File Format.

Logically, a Requirements file is just a list of pip install arguments placed in a file. Note that you should not rely on the items in the file being installed by pip in any particular order.

In practice, 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. As it is now, pip doesn’t have true dependency resolution, but instead simply uses the first specification it finds for a project. E.g. if pkg1requires pkg3>=1.0 and pkg2 requires pkg3>=1.0,<=2.0, and if pkg1 is resolved first, pip will only use pkg3>=1.0, and could easily end up installing a version of pkg3 that conflicts with the needs of pkg2. To solve this problem, you can place pkg3>=1.0,<=2.0 (i.e. the correct specification) into your requirements file directly along with the other top level requirements. Like so:

    pkg1
    pkg2
    pkg3>=1.0,<=2.0
    
  3. Requirements files are used to force pip to install an alternate version of a sub-dependency. For example, suppose ProjectA in your requirements file requires ProjectB, but the latest version (v1.3) has a bug, you can force pip to accept earlier versions like so:

    ProjectA
    ProjectB<1.3
    
  4. Requirements files are used to override a dependency with a local patch that lives in version control. For example, suppose a dependency, SomeDependency from PyPI has a bug, and you can’t wait for an upstream fix. You could clone/copy the src, make the fix, and place it in VCS with the tag sometag. You’d reference it in your requirements file with a line like so:

    git+https://myvcs.com/some_dependency@sometag#egg=SomeDependency
    

    If SomeDependency was previously a top-level requirement in your requirements file, then replace that line with the new line. If SomeDependency is a sub-dependency, then add the new line.

It’s important to be clear that pip determines package dependencies using install_requires metadata, not by discovering requirements.txt files embedded in projects.

See also:

 

https://pip.pypa.io/en/stable/user_guide/

以上是关于依赖的导出和安装requirements.txt的主要内容,如果未能解决你的问题,请参考以下文章

Python将依赖包导出到requirements.txt文件

Python项目requirements.txt依赖包如何生成?

导出django项目依赖包在新项目安装

pip/conda导出 requirements.txt 注意事项

项目依赖包导出与导入

python 项目中的 requirements.txt