Q: python3使用requests模块发https请求,提示caused by ssl error, can't connect to https url because the ssl module is not available
首先在pycharm--File--Settings--Project--Project Interpreter,添加ssl,结果安装如下报错,在命令行用 pip install ssl
也是报相同的错误 :
Looking in indexes: http://pypi.douban.com/simple
Collecting ssl
Downloading http://pypi.doubanio.com/packages/83/21/f469c9923235f8c36d5fd5334ed11e2681abad7e0032c5aba964dcaf9bbb/ssl-1.16.tar.gz
ERROR: Command errored out with exit status 1:
command: /usr/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-8cbn9rjm/ssl/setup.py'"'"'; __file__='"'"'/tmp/pip-install-8cbn9rjm/ssl/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'
'"'"', '"'"'
'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-8cbn9rjm/ssl/pip-egg-info
cwd: /tmp/pip-install-8cbn9rjm/ssl/
Complete output (6 lines):
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-install-8cbn9rjm/ssl/setup.py", line 33
print 'looking for', f
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(print 'looking for', f)?
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
一开始以为是本机安装了python2和python3导致的,因为上述错误是语法错误,print 'looking for', f
,python3的print是作为方法,不是关键字。想修改上面的setup.py,发现在/tmp/pip-install-8cbn9rjm/ssl/setup.py
找不到这个文件,估计是失败自动回滚删除了。
于是在命令行pip3 install ssl -v
打印详细信息,报错如下:
[root@ana53 yum-rpm]# pip install ssl -v
Traceback (most recent call last):
File "/usr/local/bin/pip", line 5, in <module>
from pip._internal.main import main
ImportError: No module named pip._internal.main
[root@ana53 yum-rpm]# pip3 install ssl -v
Collecting ssl
1 location(s) to search for versions of ssl:
* http://pypi.douban.com/simple/ssl/
Getting page http://pypi.douban.com/simple/ssl/
Starting new HTTP connection (1): pypi.douban.com
"GET /simple/ssl/ HTTP/1.1" 301 162
Starting new HTTP connection (1): pypi.doubanio.com
"GET /simple/ssl/ HTTP/1.1" 200 417
Analyzing links from page http://pypi.doubanio.com/simple/ssl/
Found link http://pypi.doubanio.com/packages/3a/c2/846a19d1572ec6cb8ac438d58a898de8926d32e13f0355cdf4ab00864b5f/ssl-1.15.tar.gz#md5=81ea8a1175e437b4c769ae65b3290e0c (from http://pypi.doubanio.com/simple/ssl/), version: 1.15
Found link http://pypi.doubanio.com/packages/83/21/f469c9923235f8c36d5fd5334ed11e2681abad7e0032c5aba964dcaf9bbb/ssl-1.16.tar.gz#md5=fb12d335d56f3c8c7c1fefc1c06c4bfb (from http://pypi.doubanio.com/simple/ssl/), version: 1.16
Using version 1.16 (newest of versions: 1.15, 1.16)
"GET /packages/83/21/f469c9923235f8c36d5fd5334ed11e2681abad7e0032c5aba964dcaf9bbb/ssl-1.16.tar.gz HTTP/1.1" 200 33052
Downloading http://pypi.doubanio.com/packages/83/21/f469c9923235f8c36d5fd5334ed11e2681abad7e0032c5aba964dcaf9bbb/ssl-1.16.tar.gz
Downloading from URL http://pypi.doubanio.com/packages/83/21/f469c9923235f8c36d5fd5334ed11e2681abad7e0032c5aba964dcaf9bbb/ssl-1.16.tar.gz#md5=fb12d335d56f3c8c7c1fefc1c06c4bfb (from http://pypi.doubanio.com/simple/ssl/)
Running setup.py (path:/tmp/pip-build-la62j81l/ssl/setup.py) egg_info for package ssl
Running command python setup.py egg_info
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-la62j81l/ssl/setup.py", line 33
print 'looking for', f
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(print 'looking for', f)?
Cleaning up...
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-la62j81l/ssl/
Exception information:
Traceback (most recent call last):
File "/opt/python36/lib/python3.6/site-packages/pip/basecommand.py", line 215, in main
status = self.run(options, args)
File "/opt/python36/lib/python3.6/site-packages/pip/commands/install.py", line 324, in run
requirement_set.prepare_files(finder)
File "/opt/python36/lib/python3.6/site-packages/pip/req/req_set.py", line 380, in prepare_files
ignore_dependencies=self.ignore_dependencies))
File "/opt/python36/lib/python3.6/site-packages/pip/req/req_set.py", line 634, in _prepare_file
abstract_dist.prep_for_dist()
File "/opt/python36/lib/python3.6/site-packages/pip/req/req_set.py", line 129, in prep_for_dist
self.req_to_install.run_egg_info()
File "/opt/python36/lib/python3.6/site-packages/pip/req/req_install.py", line 439, in run_egg_info
command_desc='python setup.py egg_info')
File "/opt/python36/lib/python3.6/site-packages/pip/utils/__init__.py", line 707, in call_subprocess
% (command_desc, proc.returncode, cwd))
pip.exceptions.InstallationError: Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-la62j81l/ssl/
Looking up "https://pypi.python.org/pypi/pip/json" in the cache
Returning cached "301 Moved Permanently" response (ignoring date and etag information)
Looking up "https://pypi.org/pypi/pip/json" in the cache
Current age based on date: 77343
Freshness lifetime from max-age: 900
Starting new HTTPS connection (1): pypi.org
There was an error checking the latest version of pip
Traceback (most recent call last):
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/adapters.py", line 423, in send
timeout=timeout
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/packages/urllib3/connectionpool.py", line 583, in urlopen
conn = self._get_conn(timeout=pool_timeout)
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/packages/urllib3/connectionpool.py", line 257, in _get_conn
return conn or self._new_conn()
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/packages/urllib3/connectionpool.py", line 808, in _new_conn
raise SSLError("Can't connect to HTTPS URL because the SSL "
pip._vendor.requests.packages.urllib3.exceptions.SSLError: Can't connect to HTTPS URL because the SSL module is not available.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/python36/lib/python3.6/site-packages/pip/utils/outdated.py", line 126, in pip_version_check
headers={"Accept": "application/json"},
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/sessions.py", line 488, in get
return self.request('GET', url, **kwargs)
File "/opt/python36/lib/python3.6/site-packages/pip/download.py", line 386, in request
return super(PipSession, self).request(method, url, *args, **kwargs)
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/sessions.py", line 617, in send
history = [resp for resp in gen] if allow_redirects else []
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/sessions.py", line 617, in <listcomp>
history = [resp for resp in gen] if allow_redirects else []
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/sessions.py", line 177, in resolve_redirects
**adapter_kwargs
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/sessions.py", line 596, in send
r = adapter.send(request, **kwargs)
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/cachecontrol/adapter.py", line 47, in send
resp = super(CacheControlAdapter, self).send(request, **kw)
File "/opt/python36/lib/python3.6/site-packages/pip/_vendor/requests/adapters.py", line 497, in send
raise SSLError(e, request=request)
pip._vendor.requests.exceptions.SSLError: Can't connect to HTTPS URL because the SSL module is not available.
发现是先下载了一个wget http://pypi.doubanio.com/packages/83/21/f469c9923235f8c36d5fd5334ed11e2681abad7e0032c5aba964dcaf9bbb/ssl-1.16.tar.gz
,于是自己手动下载,解压看到
└── ssl-1.16
├── Makefile
├── PKG-INFO
├── README.rst
├── setup.py
├── ssl
└── test
打开setup.py,发现第33行就是第一个错误日志里面的错误代码,发现这个包只适合python2,尝试改了里面所有print语句的语法还是会报错。
30 # Check the standard locations
31 for dir in std_dirs:
32 f = os.path.join(dir, filename)
33 print 'looking for', f
34 if os.path.exists(f):
35 return []
转移思路,以为是本机python2和python3同时存在导致,用pip下载的是python2版本的ssl包。于是百度怎么把python2卸载。于是看到了下面这篇文章
Centos7 python2.7和yum完全卸载及重装 https://www.cnblogs.com/wangjunjiehome/p/9239005.html
只看了完全卸载(卸载python和yum命令),没看重装部分。
参考的博文,注意rpm包的版本需要根据自己系统版本修改,譬如我的是CentOS Linux release 7.6.1810,rpm下载路径是http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/ (7.* 都是用7的rpm包)
1、删除现有Python
[root@test ~]# rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps ##强制删除已安装程序及其关联
[root@test ~]# whereis python |xargs rm -frv ##删除所有残余文件 ##xargs,允许你对输出执行其他某些命令
[root@test ~]# whereis python ##验证删除,返回无结果
2、删除现有的yum
[root@test ~]# rpm -qa|grep yum|xargs rpm -ev --allmatches --nodeps
[root@test ~]# whereis yum |xargs rm -frv
------------后面的部分需要根据自己本机报错去补充下载相应rpm包--------------------------
3、从http://mirrors.ustc.edu.cn/centos/6.4/os/x86_64/Packages/下载相应的包
python-2.6.6-36.el6.x86_64.rpm
python-devel-2.6.6-36.el6.x86_64.rpm
python-libs-2.6.6-36.el6.x86_64.rpm
python-pycurl-7.19.0-8.el6.x86_64.rpm
python-setuptools-0.6.10-3.el6.noarch.rpm
python-urlgrabber-3.9.1-8.el6.noarch.rpm
python-iniparse-0.3.1-2.1.el6.noarch.rpm
rpm-python-4.8.0-32.el6.x86_64.rpm
yum-3.2.29-40.el6.centos.noarch.rpm
yum-metadata-parser-1.1.2-16.el6.x86_64.rpm
yum-utils-1.1.30-14.el6.noarch.rpm
yum-plugin-fastestmirror-1.1.30-14.el6.noarch.rpm
yum-plugin-protectbase-1.1.30-14.el6.noarch.rpm
yum-plugin-aliases-1.1.30-14.el6.noarch.rpm
yum-plugin-downloadonly-1.1.30-14.el6.noarch.rpm
由于源中版本会更新,具体请查看URL中的版本再下载下来!
[root@test ~]# rpm -Uvh --replacepkgs python*.rpm
[root@test ~]# rpm -Uvh --replacepkgs rpm-python*.rpm yum*.rpm
可能之间还需要zlib和zlib-devel包,根据情况下载并安装!
结果悲剧了,yum命令找不到了(好奇过为什么要把yum卸载,不知道linux的yum是依赖python的,不卸载yum是无法卸载python2的)。没有yum命令只用源码安装python2,缺少很多依赖,只能一个个手动下载rpm包,下面是我下载的所有rpm包
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/rpm-build-libs-4.11.3-40.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/rpm-libs-4.11.3-40.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/rpm-build-4.11.3-40.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/rpm-cron-4.11.3-40.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/rpm-devel-4.11.3-40.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/rpm-plugin-systemd-inhibit-4.11.3-40.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/rpm-sign-4.11.3-40.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-2.7.5-86.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-devel-2.7.5-86.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-libs-2.7.5-86.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-pycurl-7.19.0-19.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-setuptools-0.9.8-7.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-urlgrabber-3.10-9.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-iniparse-0.4-9.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/rpm-python-4.11.3-40.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/yum-3.4.3-163.el7.centos.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/yum-utils-1.1.31-52.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-52.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/yum-plugin-protectbase-1.1.31-52.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/yum-plugin-aliases-1.1.31-52.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-rpm-macros-3-32.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-backports-1.0-8.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python2-rpm-macros-3-32.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-ipaddress-1.0.16-2.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-srpm-macros-3-32.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/rpm-4.11.3-40.el7.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/libxml2-python-2.9.1-6.el7_2.3.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-kitchen-1.1.1-5.el7.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/python-chardet-2.2.1-3.el7.noarch.rpm
安装rpm包:
rpm -Uvh --replacepkgs python*.rpm
rpm -Uvh --replacepkgs rpm-python*.rpm yum*.rpm
我的yum命令终于可以用了!!!
最后解决python3的ssl模块问题,原来是要用pyopenssl,完美解决
pip install pyopenssl
重新编译python3
./Modules/readline.c:32:31: fatal error: readline/readline.h: No such file or directory