8

我做了以下事情:

!pip install pytorch_lightning -qqq
import pytorch_lightning

但是得到以下错误:

ImportError                               Traceback (most recent call last)
<ipython-input-7-d883b15aac58> in <module>()
----> 1 import pytorch_lightning

----------------------------------9 frames------------------------------------------------
/usr/local/lib/python3.7/dist-packages/pytorch_lightning/utilities/apply_func.py in <module>()
     26 
     27 if _TORCHTEXT_AVAILABLE:
---> 28     from torchtext.data import Batch
     29 else:
     30     Batch = type(None)

ImportError: cannot import name 'Batch' from 'torchtext.data' (/usr/local/lib/python3.7/dist-packages/torchtext/data/__init__.py)

可能是什么问题?

4

5 回答 5

7

如Github上的 Issue #6415 中所述,尝试从 GitHub 安装。
它对我有用。

!pip install git+https://github.com/PyTorchLightning/pytorch-lightning
import pytorch_lightning as pl
print(pl.__version__)

输出:

1.3.0dev

似乎错误来自问题 #6210,他们说它已修复。我猜它没有上传到 PyPi。

于 2021-03-08T23:44:48.600 回答
3

示例工作环境:https ://colab.research.google.com/drive/1GSCd3Gz3EOQIln3v065VKWKbB3_F8xqK?usp=sharing

您可以在重新启动环境后尝试吗?

!pip install torchtext==0.8.0 torch==1.7.1 pytorch-lightning==1.2.2
import pytorch_lightning as pl
print(pl.__version__)
...

似乎有一个尚未触及 pip 的错误,因为 pytorch 闪电没有引用最新的 torchtext。

在此处输入图像描述

于 2021-03-08T23:11:24.327 回答
1

迟到了,但是如果您在使用 PyTorch Lightning Bolts 时遇到类似的困难,您可以在发布时使用相同的策略来解决它:

!pip install git+https://github.com/PyTorchLightning/lightning-bolts
于 2021-08-12T01:44:57.450 回答
0

似乎问题来自pytorch-lightning==1.1.x版本。1.2.x以上的版本修复了这个问题

但是,在PythonSnek的答案中采用最新版本会导致稍后在保存检查点时出现其他一些错误。这可能是因为最新版本 -1.3.0dev尚未开发。

安装其中一个稳定版本的tar.gz可以解决问题

!pip install https://github.com/PyTorchLightning/pytorch-lightning/releases/download/1.2.6/pytorch-lightning-1.2.6.tar.gz 
于 2021-04-02T20:53:50.707 回答
0

你可以试试这个命令,我遇到了同样的问题并且能够解决这个问题。

!pip install --upgrade pytorch-lightning
于 2021-04-03T01:08:25.897 回答