从 github 和 url 加载一个以 .git 结尾的包

Load a package from github and url ending in .git

我尝试从 github 导入一个 Python 包。我在 Google Colab 工作。

存储库位于以下 url https://github.com/microsoft/nlp-recipes/tree/master/utils_nlp.

所以我使用下面的代码

!pip install --upgrade 
!pip install -q git+git://github.com/microsoft/nlp-recipes/tree/master/utils_nlp
from utils_nlp import *

我也试过了

!pip install -q git+https://github.com/microsoft/nlp-recipes/tree/master/utils_nlp

我看到其他(有效的)示例,其中 url 以 .git 结尾:

!pip install -q git+https://github.com/huggingface/transformers.git

所以我反过来试了

!pip install -q git+https://github.com/microsoft/nlp-recipes/tree/master/utils_nlp.git

但我也注意到 https://github.com/microsoft/nlp-recipes/tree/master/utils_nlp.git loads to an error page while https://github.com/huggingface/transformers.git load to https://github.com/huggingface/transformers 这让我很吃惊。

我们如何在此处加载 Python 包?

编辑

我正在按照建议使用

pip install git+https://github.com/microsoft/nlp-recipes.git

然后

from utils_nlp import *

有效,但无法成功导入子文件夹,我导入时失败

from utils_nlp.models.transformers.abstractive_summarization_bertsum \
      import BertSumAbs, BertSumAbsProcessor

utils_nlp 确实包含一个文件夹 models,该文件夹又包含 transformers

然后错误堆栈如下

/usr/local/lib/python3.7/dist-packages/utils_nlp/models/transformers/abstractive_summarization_bertsum.py in <module>()
     15 from torch.utils.data.distributed import DistributedSampler
     16 from tqdm import tqdm
---> 17 from transformers import AutoTokenizer, BertModel
     18 
     19 from utils_nlp.common.pytorch_utils import (

ModuleNotFoundError: No module named 'transformers'

很奇怪 utils_nlp.models.transformers.abstractive_summarization_bertsum 中的代码没有解析对 transformers

的依赖

这是正确的安装方式:

pip install git+https://github.com/microsoft/nlp-recipes.git

不能安装模块,只能安装包。安装后你可以继续你的代码的其余部分

from utils_nlp import *
...

正如您在 setup guide 中所读到的(安装软件包时您一定要阅读):

The pip installation does not install any of the necessary package dependencies, it is expected that conda will be used as shown above to setup the environment for the utilities being used.

这解释了你的错误:transformers 包没有自动安装,你需要自己安装(只需使用 pip install transformers)。 setup.py 文件也证实了这一点:没有“install_requires”依赖项。