Skip to content

Fails on import with custom exception #637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vidartf opened this issue Jun 12, 2017 · 6 comments
Closed

Fails on import with custom exception #637

vidartf opened this issue Jun 12, 2017 · 6 comments

Comments

@vidartf
Copy link

vidartf commented Jun 12, 2017

On Windows, without git available on path, the following happens on import of GitPython:

[package stack]
    from git.exc import GitError
  File "C:\Miniconda3\lib\site-packages\git\__init__.py", line 45, in <module>
    from git.repo import Repo               # @NoMove @IgnorePep8
  File "C:\Miniconda3\lib\site-packages\git\repo\__init__.py", line 4, in <module>
    from .base import *
  File "C:\Miniconda3\lib\site-packages\git\repo\base.py", line 31, in <module>
    from git.remote import Remote, add_progress, to_progress_instance
  File "C:\Miniconda3\lib\site-packages\git\remote.py", line 190, in <module>
    class FetchInfo(object):
  File "C:\Miniconda3\lib\site-packages\git\remote.py", line 219, in FetchInfo
    v = Git().version_info[:2]
  File "C:\Miniconda3\lib\site-packages\git\cmd.py", line 461, in version_info
    return self._version_info
  File "C:\Miniconda3\lib\site-packages\git\cmd.py", line 424, in __getattr__
    return LazyMixin.__getattr__(self, name)
  File "C:\Miniconda3\lib\site-packages\gitdb\util.py", line 256, in __getattr__
    self._set_cache_(attr)
  File "C:\Miniconda3\lib\site-packages\git\cmd.py", line 444, in _set_cache_
    version_numbers = self._call_process('version').split(' ')[2]
  File "C:\Miniconda3\lib\site-packages\git\cmd.py", line 877, in _call_process
    return self.execute(call, **exec_kwargs)
  File "C:\Miniconda3\lib\site-packages\git\cmd.py", line 602, in execute
    raise GitCommandNotFound(command, err)
git.exc.GitCommandNotFound: Cmd('git') not found due to: FileNotFoundError('[WinError 2] The system cannot find the file specified')
  cmdline: git version

Since it fails while trying to import the exception type that it raises (GitError), the only way to catch the error is with a try: ... except Exception: block, which is needlessly wide. It would be nice if a better pattern was available for conditional use of the package.

@vidartf
Copy link
Author

vidartf commented Jun 12, 2017

(this was using version 2.1.5)

@vidartf
Copy link
Author

vidartf commented Jun 12, 2017

It seems like this was introduced in 2.1.4 (2.1.3 imports flawlessly even without git on path).

@scottbelden
Copy link

Wow, I was literally just about to make this ticket. At the moment my only option to conditionally import is something like:

try:
    import git
except:
    pass

But as @vidartf notes, that's less than ideal.

@Byron
Copy link
Member

Byron commented Jun 17, 2017

Thanks for reporting this issue! I believe that the problem originates here, and could be solved by delaying that particular operation until that _flag_map is actually required. This would also decrease the time it takes to import GitPython.
It looks like a decent python exercise to do that, in case one would want to contribute a PR :).

Personally I believe at the time of import, nothing but ImportErrors should be thrown, which is clearly violated here.

@kenodegard
Copy link
Contributor

A possible solution is that of #640 where import git succeeds with or without git available. This then allows the user to dynamically set their git path after import (as would be the case if a program needed to first read and parse a config file which contains the git path).

The above mentioned PR introduces a git.refresh function which allows the user to set their git path during runtime and hence refreshes both the GIT_PYTHON_GIT_EXECUTABLE in the git.cmd.Git object but also the _flag_map in the git.remote.FetchInfo object.

r-darwish pushed a commit to getslash/backslash-python that referenced this issue Aug 2, 2017
See the following issue which explains why we can't catch a specific exception type:

gitpython-developers/GitPython#637
r-darwish pushed a commit to getslash/backslash-python that referenced this issue Aug 2, 2017
See the following issue which explains why we can't catch a specific exception type:

gitpython-developers/GitPython#637
vmalloc pushed a commit to getslash/backslash-python that referenced this issue Aug 2, 2017
See the following issue which explains why we can't catch a specific exception type:

gitpython-developers/GitPython#637
@Byron
Copy link
Member

Byron commented Sep 28, 2017

This should be fixed with the latest, upcoming release, v2.1.7.

@vidartf vidartf closed this as completed Oct 24, 2017
yarikoptic pushed a commit to yarikoptic/GitPython that referenced this issue Nov 27, 2017
riley-martine pushed a commit to riley-martine/GitPython that referenced this issue Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants