无法在GIT bash mingw32 for Windows中激活virtualenv

44 python git bash virtualenv git-bash

当我尝试从GIT bash mingw32激活我的virtualenv时,我没有得到预期的响应.

注意:py是我的Google云端硬盘文件夹中的Python项目文件夹.hy是我跑的时候制作的virtualenv文件夹virtualenv --distribute hy.

s3z@s3z ~/Google Drive/py/hy
$ Scripts/activate
Run Code Online (Sandbox Code Playgroud)

所以你看,之前没有预期的(hy) s3z@s3z ~/Google Drive/py/hy

s3z@s3z ~/Google Drive/py/hy
$ Scripts/activate.bat
Scripts/activate.bat: line 1: @echo: command not found
Scripts/activate.bat: line 4: syntax error near unexpected token `('
Scripts/activate.bat: line 4: `if not defined PROMPT ('
Run Code Online (Sandbox Code Playgroud)

当定位实际文件名时,activate.bat我收到错误消息.然后我尝试停用以查看发生了什么,因为我认为也许virtualenv被激活但隐藏了.

s3z@s3z ~/Google Drive/py/hy
$ Scripts/deactivate.bat
Scripts/deactivate.bat: line 1: @echo: command not found
Scripts/deactivate.bat: line 3: syntax error near unexpected token `('
Scripts/deactivate.bat: line 3: `if defined _OLD_VIRTUAL_PROMPT ('

s3z@s3z ~/Google Drive/py/hy
$ Scripts/deactivate
sh.exe": Scripts/deactivate: No such file or directory
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Pie*_*don 87

做好Scripts/activate运行脚本的外壳,这是脚本执行后销毁的新实例.要在当前shell中运行脚本,请使用. Scripts/activatesource Scripts/activate.

Scripts/activate.bat在这里不起作用,因为它是用Batch,Windows cmd.exe语言编写的,而不是Bash.

  • 你需要单独安装`virtualenv`才能工作.Python 3内置的`python -m venv`命令不会创建相同的`activate`脚本. (4认同)
  • 有没有办法可以将它添加到Pycharm终端路径以自动调用它? (4认同)

Nat*_*ler 16

如何使用 Git Bash 激活然后停用 venv


要在Windows 10中访问Python,您需要在创建虚拟环境时添加.exe。

$ python.exe -m venv Scripts
Run Code Online (Sandbox Code Playgroud)

然后您可以进入为虚拟环境创建的脚本文件夹。

$ cd Scripts/
Run Code Online (Sandbox Code Playgroud)

现在您必须调用它来激活虚拟环境。

$ source ./Scripts/activate
Run Code Online (Sandbox Code Playgroud)

完成后,您只需停用虚拟环境即可。

$ deactivate
Run Code Online (Sandbox Code Playgroud)

如何激活/停用 Git Bash venv 的屏幕截图



Ama*_*don 7

在 Windows10 上,您可以进入放置 Scripts 文件夹的 virtualenv 目录,然后输入以下命令

source ./Scripts/activate
Run Code Online (Sandbox Code Playgroud)

请注意 .(dot) 没有这个,当我像上面提到的那样使用它时它会给我错误,然后由 Pierre 提供的解决方案为我工作。