简体   繁体   English

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

[英]Can not activate a virtualenv in GIT bash mingw32 for Windows

When I try to activate my virtualenv from GIT bash mingw32 I do not get the expected response.当我尝试从 GIT bash mingw32 激活我的 virtualenv 时,我没有得到预期的响应。

NOTE: py is the folder for Python projects in my Google Drive folder.注意: py是我的 Google Drive 文件夹中 Python 项目的文件夹。 hy is the virtualenv folder that was made when I ran virtualenv --distribute hy . hy是我运行virtualenv --distribute hy时创建的 virtualenv 文件夹。

s3z@s3z ~/Google Drive/py/hy
$ Scripts/activate

So you see, there is no expected (hy) that appears before s3z@s3z ~/Google Drive/py/hy所以你看, s3z@s3z ~/Google Drive/py/hy之前没有出现预期的(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 ('

And when targeting the actual file name activate.bat I get error messages.当定位实际文件名activate.bat时,我收到错误消息。 Then I try deactivating to see what happens cause I thought maybe virtualenv was activated but hidden.然后我尝试停用看看会发生什么,因为我认为 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

Any ideas?有任何想法吗?

Doing Scripts/activate runs the script in a new instance of the shell, which is destroyed after the script execution. Doing Scripts/activate在 shell 的一个新实例中运行脚本,该实例在脚本执行后被销毁。 To run the script in your current shell, use either . Scripts/activate要在当前 shell 中运行脚本,请使用. Scripts/activate . Scripts/activate or source Scripts/activate . . Scripts/activatesource Scripts/activate

Scripts/activate.bat does not work here because it is written in Batch, Windows cmd.exe language, and not Bash. Scripts/activate.bat在这里不起作用,因为它是用 Batch、Windows cmd.exe语言而不是 Bash 编写的。

On Windows10, you could go in the directory of your virtualenv where the Scripts folder is placed then enter the below command在 Windows10 上,您可以进入放置 Scripts 文件夹的 virtualenv 目录,然后输入以下命令

source ./Scripts/activate

Note the .(dot) without this it was giving error to me when I use it like above mentioned then the solution provided by Pierre worked for me.请注意 .(dot) 没有这个,当我像上面提到的那样使用它时它会给我错误,然后由 Pierre 提供的解决方案为我工作。

How to activate then deactivate a venv with Git Bash如何使用 Git Bash 激活然后停用 venv


To access your python in Windows 10, you need to add the .exe when creating the virtual environment.要在 Windows 10 中访问您的 python,您需要在创建虚拟环境时添加 .exe。

$ python.exe -m venv Scripts

Then you can move into the Scripts folder you created for the virtual environment.然后,您可以进入为虚拟环境创建的 Scripts 文件夹。

$ cd Scripts/

Now you have to call on it to activate the virtual environment.现在您必须调用它来激活虚拟环境。

$ source ./Scripts/activate

When you are done you just deactivate the virtual environment.完成后,您只需停用虚拟环境。

$ deactivate

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


I know there's an answer already on here but I thought I could post my solution for those coming to this post a lot later like me.我知道这里已经有一个答案,但我想我可以为那些像我这样的人稍后发布我的解决方案。 After installing python 3.8.2 and making sure you selected add python to c drive path on the initial install, do the following...安装 python 3.8.2 并确保在初始安装时选择将 python 添加到 c 驱动器路径后,执行以下操作...

  1. Install virtualenv with pip install virtualenv and then test.使用pip install virtualenv ,然后进行测试。

  2. Go into your project folder, type pip freeze this will show all the installed packages which may not be wanted for a new project.进入您的项目文件夹,输入pip freeze这将显示新项目可能不需要的所有已安装包。

  3. Type python -m venv ./venv and then cd venv ,输入python -m venv ./venv ,然后输入cd venv

  4. Type . Scripts/activate键入. Scripts/activate . Scripts/activate . . Scripts/activate . You should now see (venv) , which is the name you gave in step 3.您现在应该看到(venv) ,这是您在步骤 3 中指定的名称。

  5. cd .. back to the root of your project folder and type pip freeze to check this virtual environment if clear for a new project. cd ..回到项目文件夹的根目录并键入pip freeze以检查此虚拟环境是否为新项目清除。

(venv) user@DESKTOP MINGW64 ~/Documents/yourproject (venv) user@DESKTOP MINGW64 ~/Documents/yourproject

Just do . Scripts/activate就做. Scripts/activate . Scripts/activate in your virtual environment folder . Scripts/activate在您的虚拟环境文件夹中在此处输入图片说明

我有同样的错误:在你的 bash 运行中,这个推荐: source venv/Scripts/activate然后使用这个推荐检查它是否有效: pip -V我希望这是有用的。

Danyal Abbasi@DESKTOP-7I43AM4 MINGW64 /e/Django-projects/studybudproject/studybud/Scripts
$ . activate
(studybud)
Danyal Abbasi@DESKTOP-7I43AM4 MINGW64 /e/Django-projects/studybudproject/studybud/Scripts
$ deactivate

Danyal Abbasi@DESKTOP-7I43AM4 MINGW64 /e/Django-projects/studybudproject/studybud/Scripts
$

I recently had this problem and i found the solution for this.我最近遇到了这个问题,我找到了解决方案。 after you have done在你完成之后

$ cd Scripts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM