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).
/etc/group
jenkins:x:125:
gups:x:1000:
/etc/passwd
jenkins:x:116:125:Jenkins,,,:/var/lib/jenkins:/bin/bash
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
umask
was used during the install?/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 likedpkg -l | grep python
. Don't post it necessarily, just see if it tells you anything.