GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fails on import with custom exception · Issue #637 · gitpython-developers/GitPython

Fails on import with custom exception #637

New issue
Copy link
New issue
Copy link
Closed
Closed
Fails on import with custom exception#637
Copy link
Labels
acknowledged

Description

@vidartf
vidartf
opened on Jun 12, 2017
Issue body actions

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.

Reacted by Scott Belden and Roey Darwish Dror

Activity

  1. vidartf commented on Jun 12, 2017

    @vidartf
    vidartf
    on Jun 12, 2017
    Author
    More actions

    (this was using version 2.1.5)

  2. vidartf commented on Jun 12, 2017

    @vidartf
    vidartf
    on Jun 12, 2017
    Author
    More actions

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

  3. scottbelden commented on Jun 12, 2017

    @scottbelden
    scottbelden
    on Jun 12, 2017
    More actions

    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.

  4. Byron
    added
    acknowledged
    on Jun 17, 2017
  5. Byron commented on Jun 17, 2017

    @Byron
    Byron
    on Jun 17, 2017
    Member
    More actions

    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.

  6. kenodegard commented on Jul 13, 2017

    @kenodegard
    kenodegard
    on Jul 13, 2017
    Contributor
    More actions

    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.

  7. r-darwish
    added 2 commits that reference this issue on Aug 2, 2017

    Swallow git import exception

    6455d60

    Swallow git import exception

    e4175b8
  8. vmalloc
    added a commit that references this issue on Aug 2, 2017

    Swallow git import exception

    a69f3d2
  9. Byron commented on Sep 28, 2017

    @Byron
    Byron
    on Sep 28, 2017
    Member
    More actions

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

  10. vidartf
    closed this as completedon Oct 24, 2017
  11. yarikoptic
    added a commit that references this issue on Nov 27, 2017

    version up

    This issue will close once commit ccff34d is merged into the '0.3' branch.
    ccff34d
  12. riley-martine
    added a commit that references this issue on Dec 7, 2023

    version up

    This issue will close once commit b9b6cd8 is merged into the 'master' branch.
    b9b6cd8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    acknowledged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.