You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(venv) [root@localhost ~]# python -c 'import nacos'
Traceback (most recent call last):
File "/root/venv/lib/python3.9/site-packages/nacos/client.py", line 29, in
from urllib.request import Request, urlopen, ProxyHandler, HTTPSHandler, build_opener
ImportError: cannot import name 'HTTPSHandler' from 'urllib.request' (/root/venv/python3/lib/python3.9/urllib/request.py)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "", line 1, in
File "/root/venv/lib/python3.9/site-packages/nacos/init.py", line 1, in
from .client import NacosClient, NacosException, DEFAULTS, DEFAULT_GROUP_NAME
File "/root/venv/lib/python3.9/site-packages/nacos/client.py", line 34, in
import httplib as HTTPStatus
ModuleNotFoundError: No module named 'httplib'
背景:
交付类项目,自动安装nacos时,需要在客户机器上调用nacos的api进行输入导入等操作,因此自制了python venv。
python版本:3.6.15和3.9.7都测试过
现象:
在制作venv的机器上安装了nacos-sdk-python,在虚拟环境下使用python -c 'import nacos'能正常,但是到其他机器上venv下import nacos会报错
(venv) [root@localhost ~]# python -c 'import nacos'
Traceback (most recent call last):
File "/root/venv/lib/python3.9/site-packages/nacos/client.py", line 29, in
from urllib.request import Request, urlopen, ProxyHandler, HTTPSHandler, build_opener
ImportError: cannot import name 'HTTPSHandler' from 'urllib.request' (/root/venv/python3/lib/python3.9/urllib/request.py)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "", line 1, in
File "/root/venv/lib/python3.9/site-packages/nacos/init.py", line 1, in
from .client import NacosClient, NacosException, DEFAULTS, DEFAULT_GROUP_NAME
File "/root/venv/lib/python3.9/site-packages/nacos/client.py", line 34, in
import httplib as HTTPStatus
ModuleNotFoundError: No module named 'httplib'
排查:
通过追踪发现客户机器python3(venv虚拟环境)下urllib.request无HTTPSHandler模块,而nacos-sdk-python引入HTTPSHandler前未判断是否存在该方法,导致client.py的34行代码抛出异常(实际是29行try失败)
说明:
我用venv同时安装了ansible和nacos-sdk-python,且ansible也使用了HTTPSHandler,但ansible有一段判断代码,请问下是否nacos sdk是否也能先判断下HTTPSHandler是否存在再使用HTTPSHandler呢?
ansible代码片段:
vim /root/venv/lib/python3.9/site-packages/ansible/module_utils/urls.py
...
try:
# python3
import urllib.request as urllib_request
from urllib.request import AbstractHTTPHandler
except ImportError:
# python2
import urllib2 as urllib_request
from urllib2 import AbstractHTTPHandler
....
if hasattr(httplib, 'HTTPSConnection') and hasattr(urllib_request, 'HTTPSHandler'):
...
临时解决:
#client.py 第29和715行,删除HTTPSHandler
(venv) [root@localhost python3]# cat /root/venv/lib/python3.9/site-packages/nacos/client.py -n
...
26 try:
27 # python3.6
28 from http import HTTPStatus
29 from urllib.request import Request, urlopen, ProxyHandler, build_opener
...
715 #https_support = HTTPSHandler(context=ctx)
...
The text was updated successfully, but these errors were encountered: