使用Python调用Git命令时遇Git可执行文件错误求助
I’ve run into this exact issue before when working with GitPython in PyCharm—even after adding Git to the system PATH, the IDE doesn’t always pick up the change right away. Let’s walk through the fixes that should get your code working:
First: Restart PyCharm (Don’t Skip This!)
When you modify system environment variables like PATH, PyCharm doesn’t automatically refresh them. You’ll need to fully close and reopen the IDE so it loads the updated PATH settings. This alone fixes the problem for most people.
If Restarting Doesn’t Work: Manually Specify the Git Executable
If PyCharm still can’t find Git, you can explicitly point GitPython to the executable in one of three ways:
1. Hardcode the Path in Your Code
Add this right after importing GitPython modules to force it to use your Git executable:
from git import Git # Replace with your actual Git path (double-check this in File Explorer!) Git.refresh(git_executable="C:/Program Files/Git/bin/git.exe")
Or set the environment variable directly in code:
import os os.environ["GIT_PYTHON_GIT_EXECUTABLE"] = "C:/Program Files/Git/bin/git.exe"
2. Set the Environment Variable in PyCharm
Instead of modifying code, configure the variable directly in your run configuration:
- Go to Run > Edit Configurations
- Select your script under Python configurations
- In the Environment variables section, click the
...button - Add a new variable:
- Name:
GIT_PYTHON_GIT_EXECUTABLE - Value: Your full Git executable path (e.g.,
C:/Program Files/Git/bin/git.exe)
- Name:
- Save and run your script again
3. Verify Your Git Path is Correct
Double-check that you’re using the full path to git.exe, not just the bin folder. To find the correct path:
- Open Git Bash and run
where git(Windows) orwhich git(macOS/Linux) - Copy the full path returned (it should end with
git.exeon Windows)
Modified Working Code Example
Here’s your code updated with the explicit Git path set:
import os from git import Repo, Git # Update this path to match your Git installation git_executable_path = "C:/Program Files/Git/bin/git.exe" Git.refresh(git_executable=git_executable_path) COMMITS_TO_PRINT = 5 def print_commit(commit): print('----') print(str(commit.hexsha)) print("\"{}\" by {} ({})".format(commit.summary, commit.author.name, commit.author.email)) print(str(commit.authored_datetime)) print(str("count: {} and size: {}".format(commit.count(), commit.size))) def print_repository(repo): print('Repo description: {}'.format(repo.description)) print('Repo active branch is {}'.format(repo.active_branch)) for remote in repo.remotes: print('Remote named \"{}\" with URL \"{}\"'.format(remote, remote.url)) print('Last commit for repo is {}.'.format(str(repo.head.commit.hexsha))) if __name__ == "__main__": # Fallback to a hardcoded path if the environment variable isn't set repo_path = os.getenv('GIT_REPO_PATH') or "C:/Users/YourUsername/your-local-git-repo" repo = Repo(repo_path) if not repo.bare: print('Repo at {} successfully loaded.'.format(repo_path)) print_repository(repo) commits = list(repo.iter_commits('master'))[:COMMITS_TO_PRINT] for commit in commits: print_commit(commit) else: print('Could not load repository at {} :('.format(repo_path))
Bonus Check: GitPython Version Compatibility
While less likely, if you’re still having issues, make sure your GitPython version (2.1.7) is compatible with your installed Git version. You can try upgrading GitPython to a more recent compatible version via PyCharm’s package manager if needed.
内容的提问来源于stack exchange,提问作者DB-93

