簡體   English   中英

如何在 pipenv 環境中處理 `pip install`(不反映在 Pipfile 中,僅反映在 `pipenv graph` 中)?

[英]How to handle `pip install` inside a pipenv environment (not reflected in the Pipfile, only in the `pipenv graph`)?

如果有人不小心在 pipenv 環境中使用pip install而不是pipenv install ,則 package 不會反映在 Pipfile 和 Pipfile.lock 上的包列表中。

問題是您可能會使用 go 來部署此 Pipfile.lock,以為您擁有所需的一切,而實際上您缺少 package。

我正在查看文檔https://pipenv.pypa.io/以了解運行pip install而不是pipenv install時實際發生的情況(即使是錯誤的),但我找不到對此的解釋。

如果您運行pipenv graph ,它實際上會顯示通過 pip 安裝的包。 所以我知道 pipenv 不知何故知道這些包? 但是我需要做什么才能使這些反映在 Pipfile 中?

首先,讓我們澄清一下pipenv install命令只是pip的包裝器。 如果您使用--verbose安裝,您會看到它也只是使用pip install並將軟件包放在相同的激活虛擬環境中。 所以答案

我正在查看文檔https://pipenv.pypa.io/以了解運行pip install而不是pipenv install時實際發生的情況(即使是錯誤的)

只是pipenv特定的操作不會被執行。 這涉及更新PipfilePipfile.lock ,這是首先使用pipenv的主要原因之一,以進行確定性構建。 Pipfile您可以手動更新自己,但對於Pipfile.lock ...您不能。

如果你運行 pipenv graph 它實際上會顯示通過 pip 安裝的包!

是的,因為正如我所說,它們都只使用pip並且包的存儲位置和方式沒有什么特別之處,即pipenv不會跟蹤它安裝的包。 這些包都將存儲在lib/pythonX.Y/site-packages下的虛擬環境文件夾中,無論是使用pipenv還是普通pip 事實上,做pip install <pkg>然后pipenv uninstall <pkg>工作。

現在談談你的實際問題:

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

D Malan 關於使用pipenv clean的評論是一個好方法。 從文檔:

$ pipenv clean --help
Usage: pipenv clean [OPTIONS]

  Uninstalls all packages not specified in Pipfile.lock.

Options:
  --bare           Minimal output.
  --dry-run        Just output unneeded packages.
  ...

如前所述,您只需要運行該命令來檢查不一致之處。 添加--dry-run命令以便它只報告,而不是實際卸載它們。

然后,您可以為此編寫一個腳本,例如 Bash 腳本:

#!/usr/local/bin/bash

echo "Checking if there are packages in venv not in Pipfile.lock"

# Get packages pipenv did not find in Pipfile.lock
# NOTE:
#   Here, mapfile requires Bash 4.x syntax
#   For alternatives: https://stackoverflow.com/a/32931403/2745495
mapfile -t packages < <(pipenv clean --dry-run)

if [ ${#packages[@]} -eq 0 ]; then
    echo "All good!"
else
    echo "Found ${#packages[@]} not in Pipfile.lock!"
    for pkg in "${packages[@]}"; do
        echo "  ${pkg}"
    done
    echo ""
    echo "Check if they need to be 'pipenv install'-ed or deleted with 'pipenv clean'"

    # Make sure script exits with a non-zero code here
    exit 1
fi

在帶有pip install -ed package (例如 mypy)和pipenv install -ed package (例如:

(my-test-repo) $ pipenv graph
flake8==3.8.4
  - mccabe [required: >=0.6.0,<0.7.0, installed: 0.6.1]
  - pycodestyle [required: >=2.6.0a1,<2.7.0, installed: 2.6.0]
  - pyflakes [required: >=2.2.0,<2.3.0, installed: 2.2.0]
mypy==0.790
  - mypy-extensions [required: >=0.4.3,<0.5.0, installed: 0.4.3]
  - typed-ast [required: >=1.4.0,<1.5.0, installed: 1.4.1]
  - typing-extensions [required: >=3.7.4, installed: 3.7.4.3]

(my-test-repo) $ cat Pipfile.lock | grep mypy

(my-test-repo) $ ./check.sh 
Checking if there are packages in venv not in Pipfile.lock
Found 4 not in Pipfile.lock!
  typing-extensions
  typed-ast
  mypy
  mypy-extensions

Check if they need to be 'pipenv install'-ed or deleted with 'pipenv clean'

(my-test-repo) $ pipenv install mypy
...
✔ Success! 
Updated Pipfile.lock (e60379)!
Installing dependencies from Pipfile.lock (e60379)...
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:

(my-test-repo) $ ./check.sh 
Checking if there are packages in venv not in Pipfile.lock
All good!

為了解決問題

您可能會使用此 Pipfile.lock go 進行部署,以為您擁有所需的一切,而實際上您缺少 package。

如果您使用的是 Git,請將腳本作為git 預提交掛鈎的一部分。

預提交掛鈎首先運行,甚至在您輸入提交消息之前。 它用於檢查即將提交的快照、查看您是否忘記了某些內容、確保測試運行或檢查您需要在代碼中檢查的任何內容。 從這個鈎子中退出非零會中止提交,盡管您可以使用git commit --no-verify繞過它。

(my-test-repo) $ cp check.sh .git/hooks/pre-commit
(my-test-repo) $ chmod +x .git/hooks/pre-commit

(my-test-repo) $ git add .
(my-test-repo) $ git commit
Checking if there are packages in venv not in Pipfile.lock
Found 4 not in Pipfile.lock!
  typing-extensions
  mypy
  mypy-extensions
  typed-ast

Check if they need to be 'pipenv install'-ed or deleted with 'pipenv clean'
(my-test-repo) $

發現不一致時會中止提交,從而防止您提交可能不完整的 Pipfile.lock。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM