Python 安装包并用pip写入requirements.txt

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20006000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:14:58  来源:igfitidea点击:

Install a package and write to requirements.txt with pip

pythonpip

提问by Nick Tomlin

I'm searching for a way to install a package with pip, and write that package's version information to my project's requirements.txt file. For those familiar with npm, it's essentially what npm install --savedoes.

我正在寻找一种使用 pip 安装包的方法,并将该包的版本信息写入我项目的 requirements.txt 文件。对于那些熟悉npm 的人来说,它本质上就是npm install --save这样做的。

Using pip freeze > requirements.txtworks great, but I've found that I forget to run this, or I can accidentally include unused packages that I'd installed for testing but decided not to use.

使用pip freeze > requirements.txt效果很好,但我发现我忘记运行它,或者我可能不小心包含了我为了测试而安装但决定不使用的未使用包。

So the following psuedocode:

所以下面的伪代码:

$ pip install nose2 --save

Would result in a requirements.txt file with:

将产生一个 requirements.txt 文件,其中包含:

nose2==0.4.7

I guess I could munge the output of save to grab the version numbers, but I am hoping there is an easier way.

我想我可以修改 save 的输出来获取版本号,但我希望有一种更简单的方法。

采纳答案by dusktreader

To get the version information, you can actually use pip freeze selectively after install. Here is a function that should do what you are asking for:

要获取版本信息,您实际上可以在安装后有选择地使用 pip freeze。这是一个应该执行您要求的功能:

pip_install_save() {
    package_name=
    requirements_file=
    if [[ -z $requirements_file ]]
    then
        requirements_file='./requirements.txt'
    fi
    pip install $package_name && pip freeze | grep -i $package_name >> $requirements_file
}

Note the -i to the grep command. Pip isn't case sensitive with package names, so you will probably want that.

请注意 grep 命令的 -i。Pip 对包名不区分大小写,因此您可能需要这样。

回答by MechanisM

Just add smth like

只需添加 smth 就像

function pips() {
    echo $'\n' >> requirements.txt; pip install 
}

into your .bashrc or .bash_profile and use pips command to install package and save it's name into requirements.txt example:

进入您的 .bashrc 或 .bash_profile 并使用 pips 命令安装包并将其名称保存到 requirements.txt 示例中:

pips django-waffle

based on Akash Kothawale comment :)

基于 Akash Kothawale 评论 :)

回答by William

I've written the following bash function which I use;

我编写了以下我使用的 bash 函数;

function pip-save() {
    for pkg in $@; do
        pip install "$pkg" && {
            name="$(pip show "$pkg" | grep Name: | awk '{print }')";
            version="$(pip show "$pkg" | grep Version: | awk '{print }')";
            echo "${name}==${version}" >> requirements.txt;
        }
    done
}

This saves the canonical package name to requirements, as well as the version installed. Example usage;

这会将规范包名称保存到需求中,以及安装的版本。示例用法;

$ pip-save channels asgi_redis
# will save the following to requirements.txt (as of writing):
# ---
# channels==1.0.1
# asgi-redis==1.0.0
# ---
# note how asgi_redis is translated to its canonical name `asgi-redis`