简体   繁体   中英

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 ?

>>> 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.

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).
BUT I had gitdb already installed!
Thanks to this hint I figured out that gitdb silently failed because it was missing 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. 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. So I added it to my PATH (though it didn't have anything to do with the python) and everything worked out eventually.

MS Windows Versions of this problem can occur because of the order of Python versions in your system PATH , as it did for me. 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. I noticed it when I looked at the PATH variable and found two versions of Python being called. 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. 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:

C:> path
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. 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:

C:> set path = 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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