I am trying to run Python unittest as part of a Jenkins build. My build was failing as follows:

+ export PYTHONPATH=/usr/lib/python2.7:/usr/lib/python2.7/dist-packages:/var/lib/jenkins/jobs/GUPS_Unit_Tests/workspace/python/plugins:/var/lib/jenkins/jobs/GUPS_Unit_Tests/workspace/:/usr/share/qgis/python:/usr/share/qgis/python/plugins:/var/lib/jenkins/.qgis2/python/plugins:/var/lib/jenkins/jobs/GUPS_Unit_Tests/workspace/gupsutest
+ export PYTHONHOME=/usr
+ cd gupsutest
+ python -m unittest discover
ImportError: No module named site

As it turns out this is a file permission issue. Python is installed at /usr/lib/python2.7, which of course is owned by root.

-rwxrwxr--   1 root root  20388 Jun 22  2015 site.py
-rwxrwxr--   1 root root  19727 Feb  2 07:45 site.pyc

I can get Python to run if I add the jenkins user to the root group. This seems dangerous at worst and bad form at best.

How can I proceed? Do I reinstall Python to /usr/local/lib? Do I change the group for Python in /usr/lib? Do I allow all access to /usr/lib/python2.7?

I am using Ubuntu 14.04 with Python 2.7.6. Something else to consider, the Python code we are writing is for QGIS plugins. These have also been installed as root. Should I change the group for these as well?

EDIT: More info. The jenkins user is set up as a system user account. That seems to be contributing to the issue. I have a normal account gups which is able to run Python (and thus has access to /usr/lib/python2.7).




gups:x:1000:1000:GUPS Buildmaster,,,:/home/gups:/bin/bash

EDIT2: Interesting difference between python folder permissions. Not sure why python3.4 and python2.7 are different.

gups@gups-xxxxxxxxxx:~$ ls -la /usr/lib | grep python
-rw-r--r--   1 root root        68232 Jan 16 20:42 libqgispython.so.2.8.6
drwxrwxr--  26 root root        12288 Feb 12 08:03 python2.7
drwxr-xr-x   3 root root         4096 Feb 23  2014 python3
drwxr-xr-x  33 root root        20480 Feb  4 05:16 python3.4
drwxr-xr-x   2 root root         4096 Feb  3 08:02 python-tz

EDIT3: I've been looking into virtualenv which seems like an appropriate solution. I ran virtualenv to create an environment in my jeknins user's home directory.

sudo virtualenv --system-site-packages /var/lib/jenkins/_python

This does not include /usr/lib/python2.7/dist-packages, which is a problem.

EDIT4: Jenkins package info.

gups@gups-Satellite-U925t:~$ sudo dpkg -l | grep jenkins
ii  jenkins                                               1.650                                               all          continuous integration system
  • 1
    Those permissions look wacky. What umask was used during the install?
    – thrig
    Commented Feb 29, 2016 at 20:22
  • Default. I think this Python was installed as part of the Ubuntu installation. umask is giving me 0002. I have not changed it. Commented Feb 29, 2016 at 21:15
  • Did you do any local installations at any point? Debian (and therefore Ubuntu) is nothing is not consistent. If /usr/lib/python 2.7 has different permissions from the other python installations, that clearly points to something being wrong. I just checked my own installation, and all the python versions (in my case, python2.6, python2.7, python3, python3.4) all have the same permissions. umask gives me 0022 here. I think that's standard. Can you do something like dpkg -l | grep python. Don't post it necessarily, just see if it tells you anything. Commented Mar 2, 2016 at 16:45
  • Also, can you say which jenkins packages you have installed? Commented Mar 2, 2016 at 16:50
  • I have not done any local installations of Python. dpkg shows me quite a bit, not sure what I'm looking for. Apologies, I'm pretty new with Python, Linux admin and etc. Jenkins packages? Plugins? It's a fairly stock install. I'm just trying to get a build to run a simple Python script at this point. Commented Mar 2, 2016 at 17:05

1 Answer 1


After some additional searching and some help from a post in the Ubuntu forums, I have discovered the solution.

As indicated by thrig and Faheem Mitha, the permissions on my python2.7 folder were in fact funky.

I fixed them as so.

sudo chmod 755 /usr/lib/python2.7
sudo find /usr/lib/python2.7 -type d -exec chmod 755 {} \;
sudo find /usr/lib/python2.7/*.py -exec chmod 644 {} \;

Which I found in this stackexchange post.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .