无法使用 Python 2.7、Windows 在 Git Bash 上创建和激活虚拟环境

Cannot create and activate a virtual environment on Git Bash with Python 2.7, Windows

我在 windows 上使用 Git Bash 和 python 2.7,我想在虚拟环境中工作。

当我打字时

virtualenv venv

Bash 说

bash: virtualenv: command not found

这让我觉得没有安装 virtualenv,然后我尝试安装 virtualenv

pip install virtualenv

但是Bash又说

bash: pip: command not found

然后通过阅读这个线程 python 2.7: cannot pip on windows "bash: pip: command not found" 我发现它找不到 pip 目录,这可能与它找不到 virtualenv 目录的原因相同。 所以我指定了 pip 的路径,然后再次尝试安装 virtualenv

python -m pip install virtualenv

它安装 virtualenv 但随后告诉

DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.

所以我继续尝试通过键入

来激活我的 virtualenv
virtualenv venv

正如我所料,我得到了与之前相同的错误

bash: virtualenv: command not found

我尝试以与 pip 相同的方式解决,方法是键入

python -m virtualenv venv

这次 bash 响应

$ python -m virtualenv venv New python executable in C:\Users\Tommaso\DJANGO~1\UDEMYD~1\METAGL~2\venv\Scripts\python.exe Installing setuptools, pip, wheel... done.

所以我猜它创建了我的名为 venv 的 virtualenv,但在我的 bash 终端中我仍然在输入行的末尾看到写作“(master)”,我猜这意味着虚拟环境不活跃。 如何激活它?

已解决!
这是我所做的。

首先,通过关注 this SO answer,我发现了如何使 Git Bash 默认使用 python 3.7 而不是 python 2.7:

我用Notepad++打开文件aliases.sh,位于

C:<path where you installed Git Bash>\etc\profile.d\aliases.sh

下款

--show-control-chars: help showing Korean or accented characters

我添加了这些行,指示我要切换的 python 的两个版本位于何处

alias python2='C:/<installation_path_of_python_2.7>/Python 2.7/python.exe' 
alias python='C:/<installation_path_of_python_3.7>/Python 3.7/python.exe' 

alias pip='C:/<installation_path_of_python_3.7>/Phyton/Scripts/pip.exe' 
alias virtualenv='C:/<installation_path_of_python_3.7>/Phyton/Scripts/virtualenv.exe' 

你真的不需要最后两个,但它会对你有很大帮助,因为它使 Git Bash 无需编写 [=16] 即可调用 pip、virtualenv 和其他脚本=] 在脚本名称之前。

您可以通过输入

来检查您是否做对了
python -i

它应该 return 你指定的最新 python 版本。

然后如果我输入

python -m virtualenv venv

在当前目录安装虚拟环境

要激活它,只需输入

. venv/scripts/activate

如果有效,您应该会看到

(venv)

靠近您的活动输入线。

要停用它只需键入

deactivate

我之前遇到过同样的问题。解决方案是先使用 pip 安装 virtualenv。那么:

  1. 输入虚拟环境名称OfTheEnvironment
  2. nameOfTheEnvironment\Scripts\activate

您应该会看到如下内容:

C:\Users\bamidele\Documents\DjangoProjects>virtualenv venv

created virtual environment CPython3.7.2.final.0-64 in 15334ms
creator CPython3Windows(dest=C:\Users\bamidele\Documents\DjangoProjects\venv, clear=False, global=False)seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=C:\Users\bamidele\AppData\Local\pypa\virtualenv\seed-app-data\v1.0.1)activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator


C:\Users\bamidele\Documents\DjangoProjects>venv\Scripts\activate

(venv) C:\Users\bamidele\Documents\DjangoProjects>```

I hope this solves your problem.

在gitbash中,cd进入你项目的虚拟环境(venv)目录,使用这个命令'source ./Scripts/activate'激活虚拟环境(venv)

你可以走 'cd venv\Scripts' 路。那么:

运行命令'activate'

这 运行 在我的 Flask 项目中。