title: How does one create a Python virtualenv in Jenkins?
body: <p>I am using a Makefile to provide consistent single commands for setting up a virtualenv, running tests, etc.  I have configured my Jenkins instance to pull from a mercurial repo and then run "make virtualenv", which does this:</p>
  
  <pre><code>virtualenv --python=/usr/bin/python2.7 --no-site-packages . &amp;&amp; . ./bin/activate &amp;&amp; pip install -r requirements.txt
  </code></pre>
  
  <p>But for some reason it insists on using the system-installed pip and trying to install my package dependencies in the system site-packages rather than the virtualenv:</p>
  
  <pre><code>error: could not create '/usr/local/lib/python2.7/dist-packages/flask': Permission denied
  </code></pre>
  
  <p>If I add some debugging commands and explicitly point to the pip in my virtualenv, things get even more confusing:</p>
  
  <pre><code>virtualenv --python=/usr/bin/python2.7 --no-site-packages . &amp;&amp; . ./bin/activate &amp;&amp; ls -l bin &amp;&amp; which pip &amp;&amp; pwd &amp;&amp; ./bin/pip install -r requirements.txt
  </code></pre>
  
  <p>Which generates the following output:</p>
  
  <pre><code>New python executable in ./bin/python2.7
  Not overwriting existing python script ./bin/python (you must use ./bin/python2.7)
  Installing setuptools, pip...done.
  Running virtualenv with interpreter /usr/bin/python2.7
  </code></pre>
  
  <p>It appears Jenkins doesn't rebuild the environment from scratch for each build, which strikes me as an odd choice, but shouldn't effect my immediate issue</p>
  
  <p>The output from the "ls -l bin" shows pip to be installed in the virtualenv and executable:</p>
  
  <pre><code>-rw-r--r-- 1 jenkins jenkins    2248 Apr  9 21:14 activate
  -rw-r--r-- 1 jenkins jenkins    1304 Apr  9 21:14 activate.csh
  -rw-r--r-- 1 jenkins jenkins    2517 Apr  9 21:14 activate.fish
  -rw-r--r-- 1 jenkins jenkins    1129 Apr  9 21:14 activate_this.py
  -rwxr-xr-x 1 jenkins jenkins     278 Apr  9 21:14 easy_install
  -rwxr-xr-x 1 jenkins jenkins     278 Apr  9 21:14 easy_install-2.7
  -rwxr-xr-x 1 jenkins jenkins     250 Apr  9 21:14 pip
  -rwxr-xr-x 1 jenkins jenkins     250 Apr  9 21:14 pip2
  -rwxr-xr-x 1 jenkins jenkins     250 Apr  9 21:14 pip2.7
  lrwxrwxrwx 1 jenkins jenkins       9 Apr 10 19:31 python -&gt; python2.7
  lrwxrwxrwx 1 jenkins jenkins       9 Apr 10 19:31 python2 -&gt; python2.7
  -rwxr-xr-x 1 jenkins jenkins 3349512 Apr 10 19:31 python2.7
  </code></pre>
  
  <p>The output of "which pip" seems to want to use the correct one:</p>
  
  <pre><code>/var/lib/jenkins/jobs/Run Tests/workspace/bin/pip
  </code></pre>
  
  <p>My current working directory is what I expect it to be:</p>
  
  <pre><code>/var/lib/jenkins/jobs/Run Tests/workspace
  </code></pre>
  
  <p>But... wtf?</p>
  
  <pre><code>/bin/sh: 1: ./bin/pip: Permission denied
  make: *** [virtualenv] Error 126
  Build step 'Execute shell' marked build as failure
  Finished: FAILURE
  </code></pre>