← 返回首页
__init__: don't run refresh on import by lheckemann · Pull Request #2073 · gitpython-developers/GitPython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (4) .rst  (1) All 2 file types selected Viewed files
Conversations
Failed to load comments. Retry
Loading
Jump to
Jump to file
Failed to load files. Retry
Loading
Diff view
Unified
Split
Hide whitespace
Apply and reload
Show whitespace
Diff view
Unified
Split
Hide whitespace
Apply and reload
13 changes: 13 additions & 0 deletions doc/source/changes.rst
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
Changelog
=========

4.0.0
=====

GitPython will no longer throw an ImportError when no git executable can
be found at import time. Instead, errors are deferred until the first
attempt at using it. Consumers with special handling for
the old ImportError behaviour should instead call `git.refresh` and handle
GitCommandNotFoundErrors themselves.

See the following for all changes.
https://github.com/gitpython-developers/GitPython/releases/tag/4.0.0


3.1.45
======

Expand Down
5 changes: 0 additions & 5 deletions git/__init__.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,4 @@ def refresh(path: Optional[PathLike] = None) -> None:
GIT_OK = True


try:
refresh()
except Exception as _exc:
raise ImportError("Failed to initialize: {0}".format(_exc)) from _exc

# } END initialize git executable path
4 changes: 3 additions & 1 deletion git/cmd.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ def refresh(cls, path: Union[None, PathLike] = None) -> bool:
if mode in warn:
_logger.critical(err)
else:
raise ImportError(err)
raise GitCommandNotFound(new_git, err)
else:
err = dedent(
"""\
Expand Down Expand Up @@ -1575,6 +1575,8 @@ def _call_process(
default (especially ``as_process = False``, ``stdout_as_string = True``) and
return :class:`str`.
"""
if not self.GIT_PYTHON_GIT_EXECUTABLE:
self.refresh()
# Handle optional arguments prior to calling transform_kwargs.
# Otherwise these'll end up in args, which is bad.
exec_kwargs = {k: v for k, v in kwargs.items() if k in execute_kwargs}
Expand Down
4 changes: 4 additions & 0 deletions test/__init__.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
#
# This module is part of GitPython and is released under the
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
#
import git

git.refresh()
2 changes: 1 addition & 1 deletion test/test_git.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def test_initial_refresh_from_bad_git_path_env_error(self, case):
Git.GIT_PYTHON_GIT_EXECUTABLE = None # Simulate startup.

with mock.patch.dict(os.environ, env_vars):
with self.assertRaisesRegex(ImportError, r"\ABad git executable.\n"):
with self.assertRaisesRegex(GitCommandNotFound, r"Bad git executable."):
refresh()

def test_initial_refresh_from_good_absolute_git_path_env(self):
Expand Down
Loading
Toggle all file notes Toggle all file annotations

Footer

© 2026 GitHub, Inc.