如何在 pipelinenv 环境中处理“pip install”(未反映在 Pipfile 中,仅反映在“pipenv graph”中)?

use*_*541 6 python pip pipenv

如果有人意外使用pip install而不是pipenv install在 pipelinenv 环境中使用,则该包不会反映在 Pipfile 上的包列表中,也不会反映在 Pipfile.lock 中。

问题是你可能会使用这个 Pipfile.lock 进行部署,以为你已经拥有了所需的一切,但实际上你缺少一个包。

我正在查看文档https://pipenv.pypa.io/以了解当您运行pip install而不是pipenv install(即使是错误的)时实际发生的情况,但我找不到对此的解释。

如果你运行pipenv graph它,它实际上会显示通过 pip 安装的软件包!所以我知道 Pipenv 在某种程度上知道这些软件包。但是我需要做什么才能使这些反映在 Pipfile 中?

Gin*_*pin 5

首先,让我们澄清该pipenv install命令只是pip. 如果您使用 进行安装--verbose,您会发现它也只是使用pip install软件包并将其放入同一个激活的虚拟环境中。所以答案是

\n
\n

我正在查看文档https://pipenv.pypa.io/以了解当您运行pip install而不是运行时实际发生的情况pipenv install(即使是错误的)时实际发生的情况

\n
\n

只是 -不会执行pipenv特定操作。这包括更新PipfilePipfile.lock (这是首先使用的主要原因之一)以获得确定性构建。您可以手动更新 Pipfile,对于Pipfile.lockpipenv不能。

\n
\n

如果您运行 pipelinev graph,它实际上会显示通过 pip 安装的软件包!

\n
\n

是的,因为正如我所说,他们都只是使用pip. 两种方法都会将软件包安装在同一个虚拟环境中,并且pipenv graph只是检查相同的环境。包将存储在 下的文件夹中lib/pythonX.Y/site-packages,无论是 withpipenv还是 plain pip

\n

现在谈谈你的实际问题:

\n
\n

但是我需要做什么才能使这些反映在 Pipfile 中?

\n
\n

D Malan 的使用评论pipenv clean是一个很好的方法。来自文档:

\n
$ pipenv clean --help\nUsage: pipenv clean [OPTIONS]\n\n  Uninstalls all packages not specified in Pipfile.lock.\n\nOptions:\n  --bare           Minimal output.\n  --dry-run        Just output unneeded packages.\n  ...\n
Run Code Online (Sandbox Code Playgroud)\n

如上所述,您只需运行该命令来检查不一致的情况。添加该--dry-run命令,以便它仅报告,而不实际卸载它们。

\n

然后您可以为此创建一个脚本,例如 Bash 脚本:

\n
#!/usr/local/bin/bash\n\necho "Checking if there are packages in venv not in Pipfile.lock"\n\n# Get packages pipenv did not find in Pipfile.lock\n# NOTE:\n#   Here, mapfile requires Bash 4.x syntax\n#   For alternatives: https://stackoverflow.com/a/32931403/2745495\nmapfile -t packages < <(pipenv clean --dry-run)\n\nif [ ${#packages[@]} -eq 0 ]; then\n    echo "All good!"\nelse\n    echo "Found ${#packages[@]} not in Pipfile.lock!"\n    for pkg in "${packages[@]}"; do\n        echo "  ${pkg}"\n    done\n    echo ""\n    echo "Check if they need to be \'pipenv install\'-ed or deleted with \'pipenv clean\'"\n\n    # Make sure script exits with a non-zero code here\n    exit 1\nfi\n
Run Code Online (Sandbox Code Playgroud)\n

pip install在带有-ed 包(例如 mypy)和pipenv install-ed 包(例如 flake8)的测试环境上运行它:

\n
(my-test-repo) $ pipenv graph\nflake8==3.8.4\n  - mccabe [required: >=0.6.0,<0.7.0, installed: 0.6.1]\n  - pycodestyle [required: >=2.6.0a1,<2.7.0, installed: 2.6.0]\n  - pyflakes [required: >=2.2.0,<2.3.0, installed: 2.2.0]\nmypy==0.790\n  - mypy-extensions [required: >=0.4.3,<0.5.0, installed: 0.4.3]\n  - typed-ast [required: >=1.4.0,<1.5.0, installed: 1.4.1]\n  - typing-extensions [required: >=3.7.4, installed: 3.7.4.3]\n\n(my-test-repo) $ cat Pipfile.lock | grep mypy\n\n(my-test-repo) $ ./check.sh \nChecking if there are packages in venv not in Pipfile.lock\nFound 4 not in Pipfile.lock!\n  typing-extensions\n  typed-ast\n  mypy\n  mypy-extensions\n\nCheck if they need to be \'pipenv install\'-ed or deleted with \'pipenv clean\'\n\n(my-test-repo) $ pipenv install mypy\n...\n\xe2\x9c\x94 Success! \nUpdated Pipfile.lock (e60379)!\nInstalling dependencies from Pipfile.lock (e60379)...\n     \xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89\xe2\x96\x89 0/0 \xe2\x80\x94 00:\n\n(my-test-repo) $ ./check.sh \nChecking if there are packages in venv not in Pipfile.lock\nAll good!\n
Run Code Online (Sandbox Code Playgroud)\n

为了解决以下问题

\n
\n

你可能会使用这个 Pipfile.lock 进行部署,认为你已经拥有了所需的一切,但实际上你缺少一个包。

\n
\n

如果您使用 Git,请将该脚本作为git pre-commit hook的一部分。

\n
\n

在您输入提交消息之前,首先运行预提交挂钩。它\xe2\x80\x99s 用于检查\xe2\x80\x99s 即将提交的快照,查看您\xe2\x80\x99s 是否忘记了某些内容,以确保测试运行,或者检查您需要执行的任何操作在代码中检查。从此挂钩退出非零会中止提交,尽管您可以使用 绕过它git commit --no-verify

\n
\n
(my-test-repo) $ cp check.sh .git/hooks/pre-commit\n(my-test-repo) $ chmod +x .git/hooks/pre-commit\n\n(my-test-repo) $ git add .\n(my-test-repo) $ git commit\nChecking if there are packages in venv not in Pipfile.lock\nFound 4 not in Pipfile.lock!\n  typing-extensions\n  mypy\n  mypy-extensions\n  typed-ast\n\nCheck if they need to be \'pipenv install\'-ed or deleted with \'pipenv clean\'\n(my-test-repo) $\n
Run Code Online (Sandbox Code Playgroud)\n

当发现不一致时,提交将中止,从而防止您提交可能不完整的 Pipfile.lock。

\n