Thursday, April 28, 2022

[SOLVED] Cannot create and activate a virtual environment on Git Bash with Python 2.7, Windows

Issue

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?


Solution

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


Answered By - Tms91
Answer Checked By - Candace Johnson (WPSolving Volunteer)