Unable to import pytorch_lightning on google colab

10,754

Solution 1

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.

Solution 2

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

Solution 3

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
Share:
10,754
user15357068
Author by

user15357068

Updated on July 18, 2022

Comments

  • user15357068
    user15357068 almost 2 years

    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?

  • PythonSnek
    PythonSnek about 3 years
    fails, with an OSError
  • Avi Thaker
    Avi Thaker about 3 years
    Could you send the error? I am unable to replicate. You can also try with !pip install pytorch=1.7.1 in addition to the other commands.
  • PythonSnek
    PythonSnek about 3 years
    /usr/lib/python3.7/ctypes/__init__.py in __init__(self, name, mode, handle, use_errno, use_last_error) 362 363 if handle is None: --> 364 self._handle = _dlopen(self._name, mode) 365 else: 366 self._handle = handle
  • PythonSnek
    PythonSnek about 3 years
    Also 1.7.1 doesn't exist
  • Avi Thaker
    Avi Thaker about 3 years
    !pip install torchtext==0.8.0 torch==1.7.1 pytorch-lightning==1.2.2
  • Avi Thaker
    Avi Thaker about 3 years
    Then restart your env
  • Avi Thaker
    Avi Thaker about 3 years
  • Kyle_397
    Kyle_397 about 3 years
    For Colab users, then you can solve this by reinstalling (or upgrading) pytorch_lightning version 1.3.0dev without any dependencies except fsspec. !pip install git+https://github.com/PyTorchLightning/pytorch-lightning fsspec --no-deps --target=$nb_path
  • PythonSnek
    PythonSnek about 3 years
    Thanks, I didn't know that was possible.