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:
virtualenv --python=/usr/bin/python2.7 --no-site-packages . && . ./bin/activate && pip install -r requirements.txt
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:
error: could not create '/usr/local/lib/python2.7/dist-packages/flask': Permission denied
If I add some debugging commands and explicitly point to the pip in my virtualenv, things get even more confusing:
virtualenv --python=/usr/bin/python2.7 --no-site-packages . && . ./bin/activate && ls -l bin && which pip && pwd && ./bin/pip install -r requirements.txt
Which generates the following output:
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
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
The output from the "ls -l bin" shows pip to be installed in the virtualenv and executable:
-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 -> python2.7 lrwxrwxrwx 1 jenkins jenkins 9 Apr 10 19:31 python2 -> python2.7 -rwxr-xr-x 1 jenkins jenkins 3349512 Apr 10 19:31 python2.7
The output of "which pip" seems to want to use the correct one:
/var/lib/jenkins/jobs/Run Tests/workspace/bin/pip
My current working directory is what I expect it to be:
/var/lib/jenkins/jobs/Run Tests/workspace
But... wtf?
/bin/sh: 1: ./bin/pip: Permission denied make: *** [virtualenv] Error 126 Build step 'Execute shell' marked build as failure Finished: FAILURE
To install virtualenv, just use pip install virtualenv . To create a virtual environment directory with it, type virtualenv /path/to/directory . Activating and deactivating the virtual environment works the same way as it does for virtual environments in Python 3 (see above).
I have being using python virtualenvs with Jenkins every day in the last two years, at multiple companies and for small side projects and cannot say I found "THE" answer. Still, I hope that sharing my experience would help others save time. Hopefully I will get further feedback in order to make the decision easier.
If you use multiple builders that do need the same virtualenv, the easiest way is to dump your environment to a file and source it at the beginning of the new builder.
To ease the maintenance I am planning to investigate these:
If you hit the shebang command line limits the best thing to do is to change your jenkins home directory to just /j
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With