10

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?

Fabian Fagerholm
  • 4,099
  • 1
  • 35
  • 45
bbm
  • 130
  • 1
  • 1
  • 7
  • I am having this exact problem right now. I even upgraded to Python 3.5 but that didn't fix it. Did you ever find a solution? – Plaid Phantom Oct 12 '15 at 07:08
  • I think this is a bug. I need to use Windows 7, 8 for development on project but always install the Git Bash Shell first thing so i can avoid that clunky DOS cmd shell and get my grep, find commands, aliases etc. I am going to try and generate a venv on Linux and archive the active/deactivate for later pastes on to Windows based systems. – Brad Sturtevant Oct 17 '15 at 14:46

5 Answers5

10

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

Community
  • 1
  • 1
Nwawel A Iroume
  • 1,249
  • 3
  • 21
  • 42
  • 1
    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. – nme Jan 24 '18 at 13:24
  • @nme In git bash, please try to go to path _venvFolder/Scripts_ and execute `. activate`. This works for me.(Windows10 64bit/Py3.5) – MoeChen Jul 16 '18 at 22:29
8

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.)

RayLuo
  • 17,257
  • 6
  • 88
  • 73
  • 2
    Thank you! God damn this was driving me insane. Had an installation of 3.6.0 that was not creating the activate script. 3.7.3 works great. – Dan Mar 27 '19 at 15:37
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.

Fabian Fagerholm
  • 4,099
  • 1
  • 35
  • 45
  • That `activate` file does not work, as it contains placeholders which are presumably populated by a python script – Eric Sep 09 '16 at 20:54
1

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/
vperezb
  • 373
  • 4
  • 7
1

this worked for me:

. activate
Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50