高效工具-requirement生成和配置

文章目录

    • 项目需求
    • requirement生成方法
      • 简要介绍
      • pip freeze方法
      • pipreqs 方法
    • requirement使用方法

项目需求

在使用项目的过程中,发现大佬们的项目中自带requirement.txt文件,可以加快项目环境部署的节奏。

自己目前具备环境部署的需要,需要能够快速方便的部署环境,方案1是生成requirement.txt

requirement生成方法

简要介绍

  • freeze是python自带的pip 模块下的指令,pipreqs 模块需要安装后才能使用
  • pip freeze只能保存使用pip install指令安装在环境中的包
  • pip freeze 会保存项目中没有使用过的包(除非使用virtualenv)
  • 因此一般安装所有模块使用pip freeze方法,精简安装版使用pipreqs方法

pip freeze方法

官方介绍文档

pip freeze [options]
--r, --requirement 
Use the order in the given requirements file and its comments when generating output. This option can be used multiple times.

-l, --local
If in a virtualenv that has global access, do not output globally-installed packages.

--user
Only output packages installed in user-site.

--path 
Restrict to the specified installation path for listing packages (can be used multiple times).

--all
Do not skip these packages in the output: setuptools, distribute, wheel, pip

--exclude-editable
Exclude editable package from output.

--exclude 
Exclude specified package from the output

使用示范

pip freeze > requirements_whole.txt

pipreqs 方法

官方介绍文档

Usage:
    pipreqs [options] 

Options:
    --use-local           Use ONLY local package info instead of querying PyPI
    --pypi-server    Use custom PyPi server
    --proxy          Use Proxy, parameter will be passed to requests library. You can also just set the
                          environments parameter in your terminal:
                          $ export HTTP_PROXY="http://10.10.1.10:3128"
                          $ export HTTPS_PROXY="https://10.10.1.10:1080"
    --debug               Print debug information
    --ignore ...    Ignore extra directories
    --encoding   Use encoding parameter for file open
    --savepath      Save the list of requirements in the given file
    --print               Output the list of requirements in the standard output
    --force               Overwrite existing requirements.txt
    --diff          Compare modules in requirements.txt to project imports.
    --clean         Clean up requirements.txt by removing modules that are not imported in project.
    --no-pin              Omit version of output packages.

使用示范

pipreqs ./ --encoding=utf8 --use-local --force

requirement使用方法

在利用上面两种方法中的一种方法生成requirement后,可以直接利用requirements安装项目所需要的包

pip install -r requirements.txt

你可能感兴趣的:(高效工具,python,pycharm)