Avoid time out error Nominatim Geopy OpenStreetMap

You could make a recursive function. Just typing Python-esque pseudo-code:

from geopy.exc import GeocoderTimedOut

def do_geocode(address, attempt=1, max_attempts=5):
        return geopy.geocode(address)
    except GeocoderTimedOut:
        if attempt <= max_attempts:
            return do_geocode(address, attempt=attempt+1)

Documentation for geopy.exc.GeocoderTimedOut.

This will keep retrying the do_geocode function until it manages to return without a Timeout exception being raised. You might want to limit the number of attempts, or also set a waiting period after a failed attempt. You might also want to think about why a timeout is occurring. Geopy allows you to set the length of time to wait before raising a timeout error, for any of the geocoding methods, with the timeout keyword argument. The documentation says that "some services are consistently slow", and recommends that this be increased.