项目需求

在使用项目的过程中,发现大佬们的项目中自带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 <file>
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 <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 <package>
Exclude specified package from the output

使用示范

pip freeze > requirements_whole.txt

pipreqs 方法

官方介绍文档

Usage:
    pipreqs [options] <path>

Options:
    --use-local           Use ONLY local package info instead of querying PyPI
    --pypi-server <url>   Use custom PyPi server
    --proxy <url>         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 <dirs>...    Ignore extra directories
    --encoding <charset>  Use encoding parameter for file open
    --savepath <file>     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 <file>         Compare modules in requirements.txt to project imports.
    --clean <file>        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
Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐