2

I would like to use GitPython in the following scenario:

  • Download and extract a portable git (e.g. one of these)
  • Use this git executable to clone a repository

I want to use the portable git, no matter if git is already installed or not (the reason being this issue).


I know i can specify the git executable by setting the GIT_PYTHON_GIT_EXECUTABLE environment variable, but

  1. i've not found a way to do this on windows (setx seems to do something, but env shows no such variable afterward) apart from manually editing the system environment variables.

  2. This is supposed to be an end-user program, i cannot ship instructions like "please set an environment variable".

  3. The path is only known when the program is already running.


So my question is, how can i set the executable path manually during runtime? The following does not seem to work, it only prints git and my system's git version:

import os
import sys

# Download and extract a portable git

git_dir = r"C:\Users\Florian\Downloads\mingit-busybox\cmd"
git_bin = os.path.join(git_dir, "git")

os.putenv("GIT_PYTHON_GIT_EXECUTABLE", git_bin)
os.environ.putenv("GIT_PYTHON_GIT_EXECUTABLE", git_bin)

# Attempt with VonC's Answer, making sure that it is first in PATH
sys.path = [git_dir] + sys.path
os.pathsep.join([git_dir]) + os.pathsep + os.environ["PATH"]

# Only import git now, because that's when the path is checked!
import git
g = git.Git()
print(g.GIT_PYTHON_GIT_EXECUTABLE)
print(".".join([str(v) for v in g.version_info]))
❯ python .\gitpython_test.py
git
2.23.0 # My portable git version is 2.20.1

1 Answer 1

2

Check if, in addition of setting, you could set the PATH.
See "how to set PATH=%PATH% as environment in Python script?"

The goal is to set PATH to C:\Users\Florian\Downloads\mingit-busybox\cmd;%PATH%, meaning the git from mingit-busybox would come first.

Here's a minimal example:

git_dir = r"C:\Users\Florian\Downloads\mingit-busybox\cmd"
# Make sure it's at the beginning of the PATH
os.environ["PATH"] = os.pathsep.join([git_dir]) + os.pathsep + os.environ["PATH"]
# NOW import it
import git
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you, that seems like it could work. I will give it a try, expect a reply in ~10h
Yes, that worked! Thank you! i only had to set the path, no need to do anything else
so, i'm not sure how to phrase it, but... this ain't it. Apparently my earlier attempts had set the right environment variable, but at the time the program didn't recognize it (shell not restarted or whatever). The next day when i tried your suggestion, it worked... but once i removed the environment variable, it did not. Sorry for that. Apparently os.environ is reset every time it's imported - I'll continue to investigate
@MCO Did you set the path as mentioned in stackoverflow.com/a/36549536/6309? And can you edit the question with the exact Python code you use?
@MCO I was about to comment ;) But we have all been there.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.