无法在google colab上导入pytorch_lightning

问题描述 投票:0回答: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)

可能是什么问题?

python pytorch google-colaboratory pytorch-lightning
8个回答
12
投票

Github 上的问题 #6415 中所述,尝试从 GitHub 安装。
这对我有用。

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

输出:

1.3.0dev

该错误似乎来自 Issue #6210 并且他们说它已修复。我猜它没有上传到 PyPi。


5
投票

示例工作环境: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 lighting 未引用最新的 torchtext。

enter image description here


3
投票

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

!pip install --upgrade pytorch-lightning

1
投票

似乎问题是由 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 

1
投票

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

!pip install git+https://github.com/PyTorchLightning/lightning-bolts

0
投票

进行以下安装解决了我的问题

pip install pytorch-lightning==1.4.4

pip install omegaconf -U

pip install hydra-core --upgrade

解决方案来源:https://github.com/PyTorchLightning/pytorch-lightning/issues/7110


0
投票

也许你应该升级你的 pytorch-lightning。我的版本是1.4.0,当我升级到1.5.10时。错误消失。


0
投票

PyTorch Lightning 已更名为 Lightning。为了获得最佳支持和最快更新,请安装该版本:

$ pip install lightning

并将其导入为:

import lightning as L

由于

pytorch_lightning
是一个广泛使用的框架,更换它的名字不可能一蹴而就。因此,它仍然保留并接收更新,但官方框架是
lightning

© www.soinside.com 2019 - 2024. All rights reserved.