[英]Git Hooks inside PyCharm running in incorrect environment
我最近在 PyCharm 中啟動了一個新項目,最終使用了 anaconda 環境。 但是,在嘗試通過 PyCharm 進行第一次提交后,它似乎使用了我的原生 python,而不是 PyCharm 中設置的環境。 我多次嘗試重新啟動 PyCharm,重新啟動計算機,並重新安裝虛擬環境。
這是預提交鈎子的副本:
set -e
# Run linters and tests
source scripts/lint.sh
linter 如下:(添加了which python
以突出問題)
set -e
set -v
which python
flake8 ~project name~
mypy ~project name~
pytest -x
black --check --fast --quiet ~project name~
set +v
我正在通過 PyCharm -> VCS -> Commit 運行提交。 在 PyCharm 中,提交失敗
但是,如果我使用$ git commit -m "testing commit"
從終端運行提交,則提交有效。 它提供以下響應:
我是否設置錯誤? 我非常喜歡 PyCharm 的 VCS,並且不想在終端中使用 git。
PyCharm 不會在虛擬環境下運行 git hooks。 錯誤跟蹤器中的相關票證: https : //youtrack.jetbrains.com/issue/PY-12988
似乎前面提到的 PyCharm 票不會很快修復(自 2014 年以來就存在)。
下面的這個 hack 對我有用; 我將此添加到 PyCharm 票證中:
這是一個對我有用的有點煩人的解決方法:
- 關閉 PyCharm。
cd /your/project/dir
- 從命令行打開 PyCharm:
PYENV_VERSION="$(pyenv local | head -1)" open /Applications/PyCharm.app/
。 我使用的是 macOS,您應該根據您的操作系統調整open
命令。我每次切換項目都必須這樣做,否則
pylint
pre-commit鈎子不起作用。 如果您的項目有類似的配置(Python 版本且未使用 PyLint),只需從 CLI 運行一次 PyCharm。
這對我有用。
我收到以下錯誤:
18:37 Commit failed with error
0 file committed, 1 file failed to commit: Update pre-commit hooks
env: python3.7: No such file or directory
當我導航到我的項目存儲庫中的.git/hooks/pre-commit
時,結果是 shebang 行是#!/usr/bin/env python3.7
。
這是一個問題,因為在我的 MacOS 上調用 python3.7 會得到以下結果:
zsh: command not found: python3.7
我可以添加一個全局 python3.7,或者更新shebang。 我選擇了后者,並將shebang行更改為:
#!/usr/bin/env python3
這為我解決了這個問題。
您可以手動編輯自動生成的預提交文件(位於您的項目目錄中的.git/hooks/pre-commit
)以將路徑添加到您的虛擬環境,替換:
# start templated
INSTALL_PYTHON = 'PATH/TO/YOUR/ENV/EXECUTABLE'
和
# start templated
INSTALL_PYTHON = 'PATH/TO/YOUR/ENV/EXECUTABLE'
os.environ['PATH'] = f'{os.path.dirname(INSTALL_PYTHON)}{os.pathsep}{os.environ["PATH"]}'
以上解決方案都不適合我: Windows 10
上的PyCharm 2020.3
我所做的是重命名.git\\hooks\\pre-commit
-> .git\\hooks\\pre-commit.py
並使用下一個內容創建了一個新的.git\\hooks\\pre-commit
:
#!/bin/bash
BASEDIR=$(dirname "$0")
/c/<PATH-to-YOUR-Python>/python.exe $BASEDIR/pre-commit.py $@
作為一個魅力工作!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.