bbm bbm - 11 months ago 104
Python Question

Activating pyvenv from gitbash for windows

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

The virtualenv library for python 2.6.6 (version 13.0.1) created the following four files in the
, and

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?

Answer Source

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.