Issue
This script I've created on my local windows machine works fine.
import git
repo = C:/user/folder/repo
g = git.cmd.Git(repo)
g.execute("git config --get remote.origin.url")
But if I try to run it in our unix server(ofc I changed the repo directory), I'm getting
File "/usr/local/lib/python3.6/site-packages/git/cmd.py", line 735, in execute
raise GitCommandNotFound(command, err) from err
git.exc.GitCommandNotFound: Cmd('git') not found due to: FileNotFoundError('[Errno 2] No such file or directory: 'git config --get remote.origin.url': 'git config --get remote.origin.url'')
Based on the gitPython documentation
exceptiongit.exc.GitCommandNotFound(command, cause) Thrown if we cannot find the git executable in the PATH or at the path given by the GIT_PYTHON_GIT_EXECUTABLE environment variable
So I've tried adding the git to PATH.
vi ~/.bashrc
export GIT_PYTHON_GIT_EXECUTABLE=/usr/local/lib/python3.6/site-packages/git/__init__.py
export PATH=$PATH:$ORACLE_HOME:${LDCONFIG_HOME}:$GIT_PYTHON_GIT_EXECUTABLE
but it's still not working
Solution
reading the documentation it appears that execute uses subprocess.Popen
in which case it simply doesn't have your environment. you have several ways to address this, either use the env
keyword with a dictionary containing your PATH, or pass shell=True
(which is dangerous if user code can reach here)
so either do:
g.execute("git config --get remote.origin.url", env=os.environ)
or
g.execute("git config --get remote.origin.url", shell=True)
Answered By - Nullman Answer Checked By - Cary Denson (WPSolving Admin)