简体   繁体   中英

GitCommandNotFound in python git library

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM