如何使用 geopy 禁用 Anaconda 虚拟环境中的证书检查



在没有Conda虚拟环境的Anaconda下在Python 2.7.10中运行下面的代码时,它工作正常。那是一年前的事了。

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

如果现在在根为 Python 3.6 且使用的虚拟环境为 Python 2.7 的 Anaconda Conda 虚拟环境中运行相同的代码,则会出现此错误。

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

试图搜索SO,谷歌并检查geopy,仍然没有找到答案。在搜索过程中,似乎禁用SSL证书检查可能是一个可行的解决方案,但不知道如何。欢迎任何建议。

遇到这个网站并得到了使其再次工作的解决方法。只需在分配geolocator之前插入 try-except-else 代码即可。

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")

最新更新