'module 'git' has no attribute 'Repo' - when running as non-root user.. works as root user

As above, I wrote a gitlab pre-receive hook that runs as a 'git' user on our gitlab server. When testing it, I get the above error. However when I SSH into the server and manually run the script as root user it works fine. It seems like the git user is either pointing to the wrong module or has the wrong permissions. However see below that many of the paths etc are the same on both users.

Below are some of the troubleshooting steps I've done.

which python3 on both users points to the same python3 location.

python3 -c 'import sys; print(sys.path)' also gives the same output:

['', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']

print(git.__path__) both prints the same path on both users: _NamespacePath(['/usr/local/lib/python3.6/dist-packages/git'])

print(git.__file__) however gives two different results As git user:

<method-wrapper '__init__' of module object at 0x7fb306043188>
['__doc__', '__loader__', '__name__', '__package__', '__path__', '__spec__']

As root user:

<method-wrapper '__init__' of module object at 0x7ffbe0600278>
['Actor', 'AmbiguousObjectName', 'BadName', 'BadObject', 'BadObjectType', 'BaseIndexEntry', 'Blob', 'BlobFilter', 'BlockingLockFile', 'CacheError', 'CheckoutError', 'CommandError', 'Commit', 'Diff', 'DiffIndex', 'Diffable', 'FetchInfo', 'GIT_OK', 'Git', 'GitCmdObjectDB', 'GitCommandError', 'GitCommandNotFound', 'GitConfigParser', 'GitDB', 'GitError', 'HEAD', 'Head', 'HookExecutionError', 'IndexEntry', 'IndexFile', 'IndexObject', 'InvalidDBRoot', 'InvalidGitRepositoryError', 'List', 'LockFile', 'NULL_TREE', 'NoSuchPathError', 'ODBError', 'Object', 'Optional', 'ParseError', 'PathLike', 'PushInfo', 'RefLog', 'RefLogEntry', 'Reference', 'Remote', 'RemoteProgress', 'RemoteReference', 'Repo', 'RepositoryDirtyError', 'RootModule', 'RootUpdateProgress', 'Sequence', 'Stats', 'Submodule', 'SymbolicReference', 'TYPE_CHECKING', 'Tag', 'TagObject', 'TagReference', 'Tree', 'TreeModifier', 'Tuple', 'Union', 'UnmergedEntriesError', 'UnsupportedOperation', 'UpdateProgress', 'WorkTreeRepositoryUnsupported', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_init_externals', 'absolute_import', 'base', 'cmd', 'compat', 'config', 'db', 'diff', 'exc', 'fun', 'head', 'index', 'inspect', 'log', 'objects', 'os', 'osp', 'reference', 'refresh', 'refs', 'remote', 'repo', 'rmtree', 'safe_decode', 'symbolic', 'sys', 'tag', 'to_hex_sha', 'typ', 'types', 'util']

python3 -m site gives the same result for both users except for USER_BASE value.

sys.path = [
    '/home',
    '/usr/lib/python36.zip',
    '/usr/lib/python3.6',
    '/usr/lib/python3.6/lib-dynload',
    '/usr/local/lib/python3.6/dist-packages',
    '/usr/lib/python3/dist-packages',
]
USER_BASE: '/root/.local' (doesn't exist) #on git user this is '/home/git/.local' (exists)
USER_SITE: '/root/.local/lib/python3.6/site-packages' (doesn't exist)
ENABLE_USER_SITE: True

python3 -m site --user-site as git user

/home/git/.local/lib/python3.6/site-packages

python3 -m site --user-site as root user

/root/.local/lib/python3.6/site-packages

With regards to the code in the script:

import sys
import os
import git
import re
import yaml

Above is the imports and below is the snippet that is failing however considering this works when ran as root user I doubt the code is the issue:

repo = git.Repo(".")

Error:

Traceback (most recent call last):
  File "./hook.py", line 193, in <module>
    get_yaml(oldrev, newrev, refname)
  File "./hook.py", line 171, in get_yaml
    repo = git.Repo(".")
AttributeError: module 'git' has no attribute 'Repo'


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source