'Contextual version conflict error, Google colab

I am trying to install and run sunpy on Google colab. It requires a package astropy version 3.1 or higher. Even after installing astropy version 3.2.1 when I import sunpy it gives the following error:

ContextualVersionConflict     Traceback (most recent call last)
 <ipython-input-10-1a62dedba559> in <module>()
   ----> 1 import sunpy

5 frames
/usr/local/lib/python3.6/dist-packages/pkg_resources/__init__.py in 
resolve(self, requirements, env, installer, replace_conflicting, 
extras)
789                 # Oops, the "best" so far conflicts with a 
dependency
790                 dependent_req = required_by[req]
--> 791                 raise VersionConflict(dist, 
req).with_context(dependent_req)
792 
793             # push the new requirements onto the stack

ContextualVersionConflict: (astropy 3.0.5 
(/usr/local/lib/python3.6/dist-packages), 
Requirement.parse('astropy>=3.1'), {'sunpy'})

pip list gives the following result:

Package                  Version              
------------------------ ---------------------
absl-py                  0.8.0                
aioftp                   0.13.0               
aiohttp                  3.6.1                
alabaster                0.7.12               
albumentations           0.1.12               
altair                   3.2.0                
astor                    0.8.0                
astropy                  3.2.1 

How this can be resoloved?



Solution 1:[1]

Install astropy >= 3.1 by hand like so before sunpy:

!pip install astropy>=3.1
!pip install sunpy

Then, restart your runtime to pick up the new astropy using the Runtime -> Restart runtime menu. Finally, import sunpy.

Here's a worked example: https://colab.research.google.com/drive/1741JFtJPq37S8oEzjzvANFWBbH3rQ4Oc

enter image description here

Solution 2:[2]

Have you tried restarting the Runtime/Kernel? The error means that a new version has been installed of a Python package that was already loaded.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bob Smith
Solution 2 Lorry