简体   繁体   English

无法在python中导入git

[英]Unable to import git in python

I am facing these issues.我正面临这些问题。 Can you help me with the same ?你能帮我做同样的事情吗? Why am I seeing this error ?为什么我看到这个错误? Do I have to add anything in the requirements.txt file ?我必须在 requirements.txt 文件中添加任何内容吗?

>>> import git
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
 import git
File "git\__init__.py", line 29, in <module>
_init_externals()
File "git\__init__.py", line 23, in _init_externals
raise ImportError("'gitdb' could not be found in your PYTHONPATH")
ImportError: 'gitdb' could not be found in your PYTHONPATH

>>> from git import Repo
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
from git import Repo
File "git\__init__.py", line 29, in <module>
_init_externals()
File "git\__init__.py", line 23, in _init_externals
raise ImportError("'gitdb' could not be found in your PYTHONPATH")
ImportError: 'gitdb' could not be found in your PYTHONPATH

I already had gitdb and smmap installed so I had to reinstall them.我已经安装了gitdbsmmap ,所以我不得不重新安装它们。

You can reinstall them by running the following command in your terminal:您可以通过在终端中运行以下命令来重新安装它们:

pip3 install --upgrade --force-reinstall gitdb; pip3 install --upgrade --force-reinstall smmap

I also got the message ImportError: 'gitdb' could not be found in your PYTHONPATH (when trying to use GitPython).我还收到消息ImportError: 'gitdb' could not be found in your PYTHONPATH (当尝试使用 GitPython 时)。
BUT I had gitdb already installed!但是我已经安装了 gitdb!
Thanks to this hint I figured out that gitdb silently failed because it was missing smmap .多亏了这个提示,我发现 gitdb 默默地失败了,因为它缺少smmap
So I installed this and it worked.所以我安装了它并且它工作。

您需要安装gitdb包。

$ sudo easy_install gitdb

I had the same problem.我有同样的问题。 However, gitdb and smmap were already installed by pip.但是, gitdb 和 smmap 已经被 pip 安装了。 As I used brew to install python and its dependencies on my mac, when I checked brew doctor command, it said that my /usr/local/sbin directory is not in my PATH.当我使用 brew 在我的 mac 上安装 python 及其依赖项时,当我检查 brew doctor 命令时,它说我的 /usr/local/sbin 目录不在我的 PATH 中。 So I added it to my PATH (though it didn't have anything to do with the python) and everything worked out eventually.所以我将它添加到我的 PATH 中(尽管它与 python 没有任何关系),最终一切都解决了。

MS Windows Versions of this problem can occur because of the order of Python versions in your system PATH , as it did for me.由于系统 PATH 中 Python 版本的顺序,可能会出现此问题的MS Windows 版本,就像对我一样。 I did not realize that when I installed another program, it installed a newer version of Python for its own usage, and it appended my system PATH with the address to the newer version.我没有意识到,当我安装另一个程序时,它安装了一个更新版本的 Python 供自己使用,并将我的系统 PATH 和地址附加到更新版本。 I noticed it when I looked at the PATH variable and found two versions of Python being called.当我查看 PATH 变量并发现调用了两个版本的 Python 时,我注意到了这一点。 Windows uses the first it finds, and if the first doesn't match what your program expects, it gets confused and can't find the right path to the module. Windows 使用它找到的第一个,如果第一个与您的程序期望的不匹配,它会感到困惑并且找不到模块的正确路径。 This is what I did to resolve it:这是我为解决它所做的:


To check: an easy way to test if this is your problem is to see if the paths separated by semicolons are in the right order.检查:测试这是否是您的问题的一种简单方法是查看由分号分隔的路径是否按正确的顺序排列。 That can be seen in the System Variables of Windows or by printing your PATH variable in your CMD shell like in this example:这可以在Windows 的系统变量中看到,或者通过在 CMD shell 中打印 PATH 变量,如下例所示:

C:> path C:>路径
PATH=C:\Program Files (x86)\ Python37-32 \Scripts;C:\Program Files (x86)\Python37-32;C:\Program Files\ Python38 \Scripts;C:\WINDOWS PATH=C:\Program Files (x86)\ Python37-32 \Scripts;C:\Program Files (x86)\Python37-32;C:\Program Files\ Python38 \Scripts;C:\WINDOWS


Temporary solution: To see if it is going to fix your computer, change it in your CMD window.临时解决方案:要查看它是否会修复您的计算机,请在 CMD 窗口中进行更改。 Your variable change will be discarded when the window is closed.当窗口关闭时,您的变量更改将被丢弃。 One way to do this test is to copy the paths, move the Python references to the order they are needed, and write it back:进行此测试的一种方法是复制路径,将 Python 引用移动到所需的顺序,然后将其写回:

C:> set path = C:\WINDOWS;C:\Program Files (x86)\Python37-32;C:\Program Files\ Python38 \Scripts;C:\Program Files (x86)\ Python37-32 \Scripts\ C:>设置路径 = C:\WINDOWS;C:\Program Files (x86)\Python37-32;C:\Program Files\ Python38 \Scripts;C:\Program Files (x86)\ Python37-32 \Scripts\

Then run the Python program to see if this was your problem.然后运行 ​​Python 程序,看看这是否是您的问题。 Note that this is only an example;请注意,这只是一个示例; do not copy & paste it.不要复制和粘贴它。 Your path is customized for the programs on your computer.您的路径是为您计算机上的程序定制的。


Permanent solution: If the above test resolves your problem, you must change your System Variables to make the change permanent.永久解决方案:如果上述测试解决了您的问题,则必须更改系统变量以使更改永久生效。 For me that usually requires a reboot afterwards in order to make the variables appear in all new windows.对我来说,通常需要在之后重新启动才能使变量出现在所有新窗口中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM