【问题标题】:Load a package from github and url ending in .git从 github 和以 .git 结尾的 url 加载包
【发布时间】:2022-01-10 15:13:25
【问题描述】:

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

存储库位于以下网址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 加载到错误页面,而https://github.com/huggingface/transformers.git 加载到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

【问题讨论】:

    标签: python github package dependencies google-colaboratory


    【解决方案1】:

    这是正确的安装方式:

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

    不能安装模块,只能安装包。安装后,您可以继续使用其余代码

    from utils_nlp import *
    ...
    

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

    pip 安装不会安装任何必要的包依赖项,预计 conda 将如上所示用于设置正在使用的实用程序的环境。

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

    【讨论】:

    • 谢谢,它部分解决了,请参阅我的帖子中的编辑
    • 什么是模块与包?
    • @kiriloff 我更新了我的答案。模块和包的区别见stackoverflow.com/questions/7948494/…
    • 谢谢,您知道如何安装(例如)tokenize indicnlp,!pip install indicnlp.tokenize 似乎不会这样做。所以,指出要从哪个包安装
    • @kiriloff 请为此打开一个新问题 :)
    猜你喜欢
    • 2012-01-22
    • 1970-01-01
    • 2021-11-04
    • 2018-05-08
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 2022-01-09
    • 2017-07-24
    相关资源
    最近更新 更多