nacos-group / nacos-group/nacos-sdk-python

用虚拟环境下的python安装的nacos-sdk-python,复制到其他机器上面会报ModuleNotFoundError: No module named 'httplib'

Open
#198 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
471
Forks
155
PR merge metrics
No merged PRs in 30d

Description

背景:
交付类项目,自动安装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)
...

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with nacos/client.py around lines 26-34 and inspect the HTTPSHandler use near line 715. Reproduce the import failure in the reported Python 3.6 or 3.9 virtual-environment setup, then verify the client handles environments without HTTPSHandler while preserving the existing Python compatibility behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.