Search code examples
pythonsslanacondacondageopy

How can I disable the certificate check in Anaconda virtual environment with geopy


When running the code below in Python 2.7.10 under Anaconda with no Conda virtual environment, it works fine. That's one year ago.

from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.reverse("16.890568, 42.543554", language="en")

Got this error if running the same code now in Anaconda Conda virtual environment in which the root is Python 3.6 and the used virtual environment is Python 2.7.

GeocoderServiceError: 
  <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)>

Tried to search SO, Google and check geopy, still found no answer. During the search, it seems that to disable ssl certificate check might be a viable solution but don't know how. Welcome for any advice.


Solution

  • Come across this site and got the workaround to make it works again. Just insert the try-except-else code before assigning the geolocator.

    from geopy.geocoders import Nominatim
    
    # Disable SSL certificate verification 
    try:
        _create_unverified_https_context = ssl._create_unverified_context
    except AttributeError:
        # Legacy Python that doesn't verify HTTPS certificates by default
        pass
    else:
        # Handle target environment that doesn't support HTTPS verification
        ssl._create_default_https_context = _create_unverified_https_context
    
    location = geolocator.reverse("16.890568, 42.543554", language="en")