4

I am using Git Bash on windows with python 2.7 and I want to work in a virtual environment.

When I type

virtualenv venv

Bash says

bash: virtualenv: command not found

That makes me think virtualenv is not installed, then I try to install virtualenv

pip install virtualenv

But again Bash says

bash: pip: command not found

Then by reading this thread python 2.7: cannot pip on windows "bash: pip: command not found" I find out that it cannot find the pip directory, that maybe is the same reason for which it cannot find virtualenv directory. So I specify the path of pip and I try again to instal virtualenv

python -m pip install virtualenv

It installs virtualenv but then tells

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.

So I go forward and try to activate my virtualenv by typing

virtualenv venv

and as I expected I get the same error of before

bash: virtualenv: command not found

that I try to solve in the same way I did for pip, by typing

python -m virtualenv venv

and this time bash responds with

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

So I guess it created my virtualenv named venv, but in my bash terminal I still get the writing "(master)" at the end of the input line, that I guess it means the virtual environment is not active. How do I activate it?

Tms91
  • 3,456
  • 6
  • 40
  • 74

4 Answers4

6

Solved!
Here is what I did.

First, by following this SO answer, I found out how to make Git Bash use python 3.7 instead of python 2.7 by default:

I opened the file aliases.sh with Notepad++, located at

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

Under the paragraph

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

I added these lines indicating where the two versions of python I want to switch are located

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' 

You don't really need the last 2 ones, but it will help you a lot, since it enables Git Bash to call pip, virtualenv and other scripts without writing python -m before the name of the script.

You can check out if you did right by typing

python -i

It should return you the latest python version you specified.

Then if I type

python -m virtualenv venv

It installs the virtual environment in the current directory

To activate that, just type

. venv/scripts/activate

If it works, you should see

(venv)

near to your active input line.

To deactivate it just type

deactivate
Tms91
  • 3,456
  • 6
  • 40
  • 74
  • In the end I would recommend to use Anaconda instead of Bash, if one is using Windows. I think Bash is plenty of bugs when it runs on Windows. – Tms91 May 26 '20 at 13:52
1

I had the same problem before. The solution is to install virtualenv first using pip. Then:

  1. enter virtualenv nameOfTheEnvironment
  2. nameOfTheEnvironment\Scripts\activate

You should see something like:

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.

Wasiu
  • 41
  • 1
  • 6
0

In git bash, cd into the virtual environment(venv) directory of your project and use this command

source ./Scripts/activate 

to activate virtual environment(venv)exampe

Tms91
  • 3,456
  • 6
  • 40
  • 74
izbid
  • 108
  • 9
0

You can go

cd venv\Scripts

path. Then:

Run the command

activate

This run on my Flask project.

Tms91
  • 3,456
  • 6
  • 40
  • 74