已解决 pre-commit无法使用 WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
设备
win10 vscode conda/24.7.1 虚拟环境python版本3.9
问题
近期在做一个团队项目,项目使用了pre-commit,但是我在commit时出现报错。
[INFO] Installing environment for https://github.com/psf/black.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: CalledProcessError: command: ('C:\\Users\\YSQ\\.cache\\pre-commit\\repo1iqbheha\\py_env-default\\Scripts\\python.EXE', '-mpip', 'install', '.')
return code: 1
stdout:
Processing c:\users\ysq\.cache\pre-commit\repo1iqbheha
Installing build dependencies: started
Installing build dependencies: finished with status 'error'
stderr:
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Processing c:\users\ysq\.cache\pre-commit\repo1iqbheha
Installing build dependencies: started
Installing build dependencies: finished with status 'error'
stderr:
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
error: subprocess-exited-with-error
Processing c:\users\ysq\.cache\pre-commit\repo1iqbheha
Installing build dependencies: started
Installing build dependencies: finished with status 'error'
stderr:
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
error: subprocess-exited-with-error
pip subprocess to install build dependencies did not run successfully.
exit code: 1
[9 lines of output]
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/hatchling/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/hatchling/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/hatchling/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/hatchling/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/hatchling/
Could not fetch URL https://pypi.org/simple/hatchling/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/hatchling/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is
not available.")) - skipping
ERROR: Could not find a version that satisfies the requirement hatchling>=1.8.0 (from versions: none)
ERROR: No matching distribution found for hatchling>=1.8.0
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error
pip subprocess to install build dependencies did not run successfully.
exit code: 1
See above for output.
note: This error originates from a subprocess, and is likely not a problem with pip.
报错的大概意思是python中缺少ssl模块,导致pip无法使用https连接。
推测
根据可能造成问题的原因依次检查:
1.conda版本过旧
(conda版本一开始是23年的,commit不了后更新到最新版本24.7.1,依旧不行)
2.使用pre-commit没有初始化
pre-commit install
(仍然失败)
3.缺少ssl配置或网络连接问题
(用脚本检查,若打印出 “SSL connection successful!" 以及状态码200,说明python环境支持SSL,并且能够通过https连接到网站。这里开了代理成功连接到了。)
4.使用的pip不正确
get-command pip
(使用的pip就是conda的pip,没有问题)
5.代理影响了commit
(不管开不开代理,或者切换节点,都报一样的错)
6.pre-commit版本太高
(下载依赖没有指定pre-commit的版本,下载的是3.8的版本,咨询师兄建议版本降到3.3.1。 重新下载并初始化后,仍然不行)
7.interpreter选择错误
使用的就是conda\envs\虚拟环境下的python(3.9.19),没有问题。
排除了大部分可能性后,推测是我的电脑的某些配置更改了,导致无法使用pre-commit 。
用docker测试
1 安装Docker桌面版
在https://www.docker.com/官网下载桌面版软件。
安装后打开,每次使用docker都需要先运行桌面版软件docker desktop。
2 拉取一个支持python3.9的linux的镜像
docker pull python:3.9
这是一个官方的python镜像,是一个轻量级的、自包含的linux环境,无需额外下载。更多的镜像可以在https://hub.docker.com/ 里查看。
3 运行Docker容器
docker run -it --name my-python-app python:3.9 /bin/bash
这个命令会创建并启动一个名为 my-python-app
的容器,并打开一个交互式 bash 会话
-it 创建一个交互式的、带有伪终端的容器 (与 /bin/bash 连用)
这里使用的镜像是python:3.9
4 在容器里克隆项目代码
git clone ...
5 使用vim编辑一个python文件
用ls
命令查看当前目录,cd
进入项目目录。
用vim
创建一个测试文件test.py。
vim test.py
若出现command not found的报错,原因是没有下载vim
# vim test.py
bash: vim: command not found
更新包列表并安装vim
apt-get update
apt-get install -y vim
完成后就可以使用vim命令来编辑文件了。
如何用vim编辑文件
- 启动 Vim: 在终端中输入
vim
命令,后面跟上你想要编辑的文件名。如果文件不存在,Vim 将创建一个新文件。
vim ftest.py
-
命令模式: 当你首次打开 Vim 时,你处于命令模式(也称为“普通模式”)。在这个模式下,你可以移动光标,复制、粘贴文本,或者执行其他不涉及直接编辑文本的操作。
-
插入模式: 要开始编辑文件,你需要切换到插入模式。按
i
键进入插入模式,在文件中的光标位置开始输入文本。如果你想在当前行的末尾添加文本,可以按A
键。 -
编辑文本: 在插入模式中,你可以像在其他文本编辑器中一样输入和编辑文本。
-
保存文件: 完成编辑后,按
Esc
键退出插入模式,返回命令模式。然后,你可以保存文件。要保存但不退出 Vim,输入:w
,然后按Enter
键。要保存并退出 Vim,输入:wq
,然后按Enter
键。 -
退出 Vim: 如果你不想保存更改,可以输入
:q
退出 Vim。如果文件已被修改,这将提示一个错误。要强制退出而不保存更改,可以使用:q!
。
在命令行里查看文件
cat test.py
6 下载pre-commit 3.3.1
pip install pip --upgrade
pip install pre-commit==3.3.1
7 初始化pre-commit
pre-commit install
8 commit和push
9 退出、停止或删除容器
exit #退出容器但不停止
docker stop my-python-app #停止容器但不删除
docker rm my-python-app #删除容器
总结
利用docker测试push,可以成功推送。所以是我的电脑的系统某些设置冲突了,导致无法利用pre-commit来检测并commit,用docker代替anaconda可以绕开这个问题。