在 Visual Studio Code 中为 git 命令激活 virtualenv

use*_*217 5 python virtualenv githooks visual-studio-code

我在 Vagrant 虚拟机 (ubuntu 18.04.5 LTS) 上有一个 Python 项目,我使用 Visual Studio Code 通过远程 SSH 扩展在其上进行开发。

当我启动时,git commit我需要在虚拟环境中运行它,因为有一个 git hook 需要它。

在工作区配置文件中,我设置了"python.pythonPath"密钥,但 git 似乎忽略了它。

我该如何解决?谢谢

all*_*lee 2

只需添加. /path/to/your/python/virtual_env/bin/activate到您的 git hooks 文件中:

.git/hooks/pre-commit
.git/hooks/pre-push
.git/hooks/post-checkout
Run Code Online (Sandbox Code Playgroud)

在您的情况下,结果.git/hooks/pre-commit可能如下所示:

#!/usr/bin/sh
. ./venv/bin/activate
exec dvc git-hook pre-commit $@
Run Code Online (Sandbox Code Playgroud)

. ./venv/bin/activate源虚拟环境。