pythonwindows-7pathvirtualenvsystem-paths

virtualenv activation on windows


I have installed virtualenv 1.10 on a Windows 7 machine with Python 2.7 on it.

I have created a virtual env called TESTENV. It is located in C:\

If I run C:\TESTENV\Scripts\activate and then type python followed by:

import sys
print sys.prefix

it outputs C:\TESTENV, as exprected.

But if I run D:\virtualenv_test.py (which is a script containing the two lines of code I used above) it outputs C:\Python27.

I tried adding a shebang line to the script but that doesn't work on Windows.

Why is the script not running in the context of the virtual environment?


Solution

  • virtualenv doesn't change the default application that starts a file.

    When you installed Python, the installation associated .py and .pyw files with appropriate C:\Python27\python.exe and C:\Python27\pythonw.exe applications. So when you run (in your cmd) only D:\virtualenv_test.py, cmd looks into the registry and finds the application that is associated with .py files and then runs the file with that application. In this case that is the previously installed C:\Python27\python.exe.

    virtualenv activate only adds that particular virtual environments python interpreter at the start of the PATH environment variable, so when you type python in cmd you will get the appropriate python.exe executed.

    So the solution to your problem is to run your script as:

    python D:\virtualenv_test.py
    

    After you activated the environment.