简体   繁体   English

使用 pipenv 检查软件包是否安装

[英]Check if package is installed using pipenv

Is there a way to check if a particular package is installed using pipenv?有没有办法检查是否使用 pipenv 安装了特定包? I have a Makefile which runs pipenv run <command> but I want to show a friendly message if the package is not installed and ask users to run pipenv install .我有一个运行pipenv run <command>Makefile但我想在没有安装软件包时显示一条友好的消息并要求用户运行pipenv install

Currently the run command just creates new vitrualenv and just fails with command not found.目前运行命令只是创建新的vitrualenv并且只是因为找不到命令而失败。

You can see what dependencies are installed in two ways : pipenv run pip freeze or pipenv graph .您可以通过两种方式查看安装了哪些依赖项: pipenv run pip freezepipenv graph

You can then use grep on the output of this.然后,您可以grep的输出使用grep grep returns 0 if it matches something, or non-zero if it doesn't, and you can check the return value by examining $? grep返回0,如果它匹配的东西,或者如果它不不为零,并且您可以检查返回值通过检查$? . .

Putting it all together, you can do something like this:把它们放在一起,你可以做这样的事情:

pipenv run pip freeze | grep mypackage
if [ $? -ne 0 ]; then
    echo "mypackage isn't installed; run pipenv install mypackage"
fi

A solution can be to call the interpreter with a minimal code and then check the return code and based on the code display the message you want.一种解决方案是使用最少的代码调用解释器,然后检查返回代码并根据代码显示您想要的消息。

Something like this:像这样的东西:

all:
    @python -c "import mypackage" 2>/dev/null|| (echo "";echo "error message";echo ""; exit 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM