user15357068
user15357068

Reputation: 191

Unable to import pytorch_lightning on google colab

I have done the following:

!pip install pytorch_lightning -qqq
import pytorch_lightning

But get the following error:

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)

What could the issue be?

Upvotes: 17

Views: 44973

Answers (8)

Bex T.
Bex T.

Reputation: 1786

PyTorch Lightning has been renamed to Lightning. For best support and fastest updates, install that version:

$ pip install lightning

and import it as:

import lightning as L

Since pytorch_lightning is a widely used framework, replacing its name can't be done overnight. For this reason, it is still kept and receives updates but the official framework is lightning.

Upvotes: 0

lawsonabs
lawsonabs

Reputation: 1

may be you should upgrade your pytorch-lightning. My version is 1.4.0, when I upgrade it to 1.5.10. The error disappears.

Upvotes: 0

drorhun
drorhun

Reputation: 584

Doing the following installations solved my problem

pip install pytorch-lightning==1.4.4

pip install omegaconf -U

pip install hydra-core --upgrade

Source of Solution: https://github.com/PyTorchLightning/pytorch-lightning/issues/7110

Upvotes: 0

PatrickAllenCooper
PatrickAllenCooper

Reputation: 21

Late, but if you are having similar difficulties with PyTorch Lightning Bolts you can solve it at the time of posting by making use of the same strategy:

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

Upvotes: 1

NouRed
NouRed

Reputation: 61

You can try this command, I encountered the same problem and was able to fix the problem.

!pip install --upgrade pytorch-lightning

Upvotes: 3

typhon04
typhon04

Reputation: 2624

Seems like the problem arises from the pytorch-lightning==1.1.x versions. Version above 1.2.x fixes the problem

But taking the latest version as in PythonSnek's answer resulted in some other bugs later on with the checkpoints saving. This could be because the latest version - 1.3.0dev is not still in development.

Installing the tar.gz of one of the stable versions fixes the problem

!pip install https://github.com/PyTorchLightning/pytorch-lightning/releases/download/1.2.6/pytorch-lightning-1.2.6.tar.gz 

Upvotes: 1

PythonSnek
PythonSnek

Reputation: 584

As said in Issue #6415 on Github, try installing from the GitHub.
It worked for me.

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

Output:

1.3.0dev

It seems that the error is coming from Issue #6210 and they say it was fixed. I guess it wasn't uploaded to PyPi.

Upvotes: 12

Avi Thaker
Avi Thaker

Reputation: 453

Example working env: https://colab.research.google.com/drive/1GSCd3Gz3EOQIln3v065VKWKbB3_F8xqK?usp=sharing

Can you try after restarting your env.

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

There appears to be a bug that has not hit pip yet with pytorch lightning not referencing the newest torchtext.

enter image description here

Upvotes: 5

Related Questions