Python git模块无效的git存储库错误 [英] Python git module Invalid git repository error
问题描述
我有一个放置在文件夹结构中的脚本,如下所示:
I have a script that is placed in a folder structure as such:
~/wofc/folder1/folder2/script.py
script.py
使用git模块执行一些任务.但是,当我从folder2
外部运行脚本时,即当我将cd
放入folder1
时,我运行python folder2/script.py arg1 arg2
我会收到raise InvalidGitRepositoryError(epath)
错误.当我从folder2
内部(即cd
到folder2
)运行python script.py arg1 arg2
时,脚本运行良好.以下是相关的代码段.您能告诉我是什么问题吗?
script.py
uses the git module to go some tasks. However when I run the script from outside of folder2
i.e. when I have cd
into folder1
i run python folder2/script.py arg1 arg2
I get the raise InvalidGitRepositoryError(epath)
error. The script runs fine when I run it from inside folder2
i.e. cd
into folder2
and run python script.py arg1 arg2
. Below is the relevant code snippet. Can You please let me know what is the issue?
git = Repo('{}/..'.format(os.getcwd())).git
git.checkout('master')
git.pull()
推荐答案
问题是您使用os.getcwd()
返回当前工作目录.如果您站在folder2
外面,则此函数将返回~/wofc/folder1
.
The problem is that you use os.getcwd()
which returns the current working directory. If you stand just outside folder2
this function will return ~/wofc/folder1
.
您应该将其替换为类似的内容:
You should swap it for something like:
import os
os.path.dirname(os.path.abspath(__file__))
例如这样的
import os
path = os.path.dirname(os.path.abspath(__file__))
git = Repo('{}/..'.format(path)).git
git.checkout('master')
git.pull()
这篇关于Python git模块无效的git存储库错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!