0

I first entered the following codes,

mkdir project #create a directory on current path called project
cd project
virtualenv my_env #create a virtual enviroment called my_env under directory project 
my_env\Scripts\activate.bat #activate my_env

then I tried

pip install requests

turns out package requests is created in the local environment, i.e., under path ...project\env\Lib\site-packages, however, I saw in a python tutorial that this code would actually create requests globally, and in order to create requests locally, one needs to use

my_env\Scripts\pip.exe install requests

So I wonder why pip install requests also performs locally in my case? Is the tutorial wrong? You can refer to the tutorial at http://thepythonguru.com/python-virtualenv-guide/

Nicholas
  • 2,560
  • 2
  • 31
  • 58
  • I don't understand. You say you would need to use `my_env\Scripts\activate.bat`, but in your first example you *did* use `my_env\Scripts\activate.bat`. So what is it that happened that you find unexpected? – BrenBarn Jan 10 '16 at 22:17
  • try running `my_env\Scripts\deactivate.bat` and then running `import requests` in a Python shell. I doubt it will work, because from what you've described it sounds like you've set up your virtual environment and activated it correctly. To get back in the env, simply run `my_env\Scripts\activate.bat` again. – cderwin Jan 10 '16 at 22:21
  • @BrenBarn, it should be "my_env\Scripts\pip.exe install requests" as the tutorial suggests. Sorry for the typo. I just corrected this mistake. – Nicholas Jan 10 '16 at 22:32
  • @cderwin, please reread my question, I made a typo and sorry for this. – Nicholas Jan 10 '16 at 22:33
  • Possible duplicate of [Can I install Python windows packages into virtualenvs?](http://stackoverflow.com/questions/3271590/can-i-install-python-windows-packages-into-virtualenvs) –  Jan 11 '16 at 06:25
  • The question's not asking if he can, he's asking why it works the way it does (in particular about how activate changes the PATH). Not a duplicate for that question IMO. – cderwin Jan 11 '16 at 07:00

1 Answers1

2

When your operating system (in this case windows) searches for an executable when it's not given it's path (like pip), it searches each of the directories in the PATH environment variable for that executable (i.e. say you enter the command python3 when

PATH="C:\Python27\bin;C:\Program Files\git\bin;
      C:\Program Files (x86)\Java\JDK\6.0\bin;C:\Python35\bin"

then the command line will search each of those directories for an executable name python3. However, it won't find the executable until it searches the last directory, and then it will run C:\Python35\bin\python3.exe.

When you install a virtual environment (venv, for short) a script is created called C:/path/to/my/venv/bin/activate.bat which you can run to activate (what a surprise!) the venv. Among other things, that script prepends C:\path\to\my\venv\bin to the PATH (i.e. sets PATH="C:\path\to\my\venv\bin;$PATH, where the dollar sign just means use the value of the PATH variable.

Then, when you ran pip install requests, the command line started to search the PATH for a pip executable. It tries entries in order, so it found pip.exe in the C:\path\to\my\venv\bin directory, so in fact you did inadvertently use C:\path\to\my\venv\bin\pip.exe when you ran pip install requests. More importantly, this is actua;;y one of the main purposes the activate.bat script exists; this way, you don't need to specify the precise path to every executable you have installed in you venv instead of using C:\path\to\my\venv\bin\python you can just use python, so long as you activate the venv. If you never run C:\path\to\my\venv\bin\activate.bat then the PATH environment variable will be as it was in the beginning, and it will install packages globally (which is not what you want).

cderwin
  • 425
  • 4
  • 11
  • Many thanks. So is it right that: if I didn't use and activate virtual environment, then "pip install requests" would execute globally. If I had activated virtual environment (i.e., after enter command "my_env\Scripts\activate.bat"), then both "pip install requests" and "my_env\Scripts\pip.exe install requests" would execute locally (honestly, I kind of feel that using "pip install requests" is easier and hence better, am I right)? – Nicholas Jan 12 '16 at 01:15
  • @NicholasLiu yes you got it – Cody Jan 12 '16 at 08:02