简体   繁体   中英

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

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?

Solved!
Here is what I did.

First, by following

answer May 21 at 17:07 (Harry) of thread Python not working in the command line of git bash

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, located here

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

with Atom text editor.

Under the paragraph

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

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

alias python='C:/<installation_path_of_python_2.7>/Python 2.7/python.exe' 
alias python2='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 pip='C:/<installation_path_of_python_3.7>/Phyton/Scripts/virtualenv.exe' 

You don't really need the last ones but it will help you a lot since it enables git 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

(Note that there is a space between the dot and the slash)

If it works, you should see

(venv)

near to your active input line.

To deactivate it just type

deactivate

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.

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) 例子

You can go

cd venv\Scripts

path. Then:

Run the command

activate

This run on my Flask project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM