MacOS虚拟环境下Astroquery查询Gaia目录失败问题排查
问题:MacOS虚拟环境中astroquery查询Gaia星表SSL证书验证失败
在MacOS本地venv虚拟环境中运行astroquery查询Gaia星表的代码时,触发SSL证书验证失败错误,但关闭虚拟环境后代码能正常执行。
相关代码
def gaia_table(ra, dec, radius): #生成用于GAIA搜索的SQL语句 query = "SELECT TOP 2000 \ gaia_source.ra,gaia_source.dec,gaia_source.ra_error,gaia_source.phot_g_mean_flux_error,gaia_source.phot_bp_mean_flux_error,gaia_source.phot_rp_mean_flux_error,gaia_source.ref_epoch,gaia_source.dec_error,gaia_source.parallax,gaia_source.parallax_error,gaia_source.radial_velocity,gaia_source.radial_velocity_error,gaia_source.pmra,gaia_source.pmra_error,gaia_source.pmdec,gaia_source.pmdec_error,gaia_source.phot_g_mean_mag,gaia_source.phot_bp_mean_mag,gaia_source.phot_rp_mean_mag,gaia_source.phot_g_mean_flux,gaia_source.phot_bp_mean_flux,gaia_source.phot_rp_mean_flux \ FROM gaiadr3.gaia_source \ WHERE \ CONTAINS( \ POINT('ICRS',gaiadr3.gaia_source.ra,gaiadr3.gaia_source.dec), \ CIRCLE('ICRS', " + str(ra) + "," + str(dec) + "," + str(((radius/2) * 0.000277778) - 0.000277778)+ ")" \ ")=1" #在线执行GAIA数据库SQL查询 job = Gaia.launch_job_async(query) results = job.get_results() return results
错误信息
File "WRAP.py", line 981, in gaia_table job = Gaia.launch_job_async(query) File "/Users/Documents/GitHub/WRAP/wrap_mac/lib/python3.8/site-packages/astroquery/gaia/core.py", line 903, in launch_job_async return TapPlus.launch_job_async(self, query=query, File "/Users/Documents/GitHub/WRAP/wrap_mac/lib/python3.8/site-packages/astroquery/utils/tap/core.py", line 409, in launch_job_async response = self.__launchJob(query, File "/Users/Documents/GitHub/WRAP/wrap_mac/lib/python3.8/site-packages/astroquery/utils/tap/core.py", line 625, in __launchJob response = self.__connHandler.execute_tappost(subcontext=context, File "/Users/Documents/GitHub/WRAP/wrap_mac/lib/python3.8/site-packages/astroquery/utils/tap/conn/tapconn.py", line 273, in execute_tappost return self.__execute_post(context, data, content_type, verbose) File "/Users/Documents/GitHub/WRAP/wrap_mac/lib/python3.8/site-packages/astroquery/utils/tap/conn/tapconn.py", line 426, in __execute_post conn.request("POST", context, data, self.__postHeaders) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1255, in request self._send_request(method, url, body, headers, encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1301, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1250, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1010, in _send_output self.send(msg) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 950, in send self.connect() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1424, in connect self.sock = self._context.wrap_socket(self.sock, File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 500, in wrap_socket return self.sslsocket_class._create( File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1040, in _create self.do_handshake() File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1309, in do_handshake self._sslobj.do_handshake() ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1125)
排查与解决办法
- 虚拟环境未继承系统根证书:MacOS系统自带的Python会加载系统根证书,但venv创建的虚拟环境可能没继承。解决方法:
- 找到系统Python的certifi证书路径,比如
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/certifi/cacert.pem - 在虚拟环境中设置环境变量指向该证书,或者在代码开头添加:
import os os.environ['REQUESTS_CA_BUNDLE'] = '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/certifi/cacert.pem'
- 找到系统Python的certifi证书路径,比如
- 临时关闭SSL验证(仅测试用):如果只是临时测试,可以在初始化Gaia时关闭证书验证:
from astroquery.gaia import Gaia Gaia.TAP.disable_ssl_verification() - 更新虚拟环境中的certifi库:certifi提供根证书,更新到最新版本可能修复证书链问题:
pip install --upgrade certifi - 核对虚拟环境Python版本:确保虚拟环境的Python版本和系统版本一致,避免证书路径不匹配。
内容的提问来源于stack exchange,提问作者Hunter Brooks
相关产品推荐
相关产品推荐

