Activating pyvenv from gitbash for windows

你说的曾经没有我的故事 提交于 2019-12-03 17:36:33

问题


I use gitbash on windows (7, 64bit). I tried to create a venv using python 3.4's built-in venv module by calling python -m venv venv, and it created successfully, but the resultant venv does not contain a bash activate script, only a .bat and .ps1.

The virtualenv library for python 2.6.6 (version 13.0.1) created the following four files in the venv/Scripts/ folder: activate, activate.bat, activate.ps1, and activate_this.py.

As the gitbash console cannot use the bat or powershell scripts, and I don't really want to have to move back to the windows cmd prompt or to a VM, is there any way I can get pyvenv to create a shell script for me while I'm on windows?


回答1:


There were lots of confusion in many answers and comments here. Some of them said you can simply do . activate, some (@nme) said "You'll get the "No such file or directory" error then since there is only activate.bat and Activate.ps1 which both don't work."

The problem did exist, as documented in this issue. And that issue was eventually fixed at Jan 2017 in Python 3.5.x, 3.6.x onward. Therefore, ONLY Python 3.5.3+, 3.6.1+, 3.7 released AFTER Jan 2017 would have fixed such issue. Possibly Python 2.7.14+ too. This means, if you are still reading this Q&A page, the first thing you would need to do is to check which version of Python you are using, and then probably do an upgrade. (For example, I encountered same issue with one of my old Python 3.6.0 environment, and after upgrading to Python 3.7.2, the problem is gone.)




回答2:


You do not have to worry about the presence of virtual env bash script go to Git bash and use . Scripts/activate or source Scripts/activate as mentioned in answer in this topic Can not activate a virtualenv in GIT bash mingw32 for Windows




回答3:


This is (currently) by design.

In Windows, only the Command Prompt (.bat) and PowerShell (.ps1) scripts are installed. The venv documentation says "The invocation of the script is platform-specific" – see the table in that document listing commands to activate the venv on different platforms. If you look at the venv source code, you can see that it differentiates between Windows and POSIX environments and installs scripts accordingly.

However, there is an open bug that requests that scripts for other shells should also be installed. While waiting for that to possibly be resolved, there is a workaround: you could grab the bash (or other shell) script from your local machine or from the cPython hg repository (direct link to the file) and put it in the Scripts folder next to the Windows-specific ones. You need to change __VENV_DIR__ to the directory where your venv is located, __VENV_BIN_NAME__ to "bin", and __VENV_PROMPT__ to whatever you want the bash prompt to be when the venv is activated (for example, "(env) "). Set permissions, and you should be good to go.




回答4:


The following code explain how to set up and start a virtualenv named env_project inside a folder named project in Git Bash:

mkdir project
cd project/
virtualenv env_project
. activate env_project/



回答5:


this worked for me:

. activate


来源:https://stackoverflow.com/questions/30577271/activating-pyvenv-from-gitbash-for-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!