Python - ImportError: cannot import name 'html5lib' from 'pip._vendor'
이런 파이썬 환경에서,
$ ls /usr/lib/python* -dl
drwxr-xr-x 26 root root 20480 Oct 30 19:34 /usr/lib/python2.7
drwxr-xr-x 3 root root 4096 Feb 20 2021 /usr/lib/python3
drwxr-xr-x 37 root root 12288 Nov 15 14:51 /usr/lib/python3.11
drwxr-xr-x 30 root root 20480 Jul 17 09:35 /usr/lib/python3.8
drwxr-xr-x 5 root root 4096 Feb 20 2021 /usr/lib/python3.9
python3.11의 -m 옵션을 이용해 pip을 실행시켰더니 다음과 같은 오류만 발생합니다.
$ python3.11 -m pip install Flask=2.2.2
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/usr/lib/python3/dist-packages/pip/__main__.py", line 19, in <module>
sys.exit(_main())
^^^^^^^
File "/usr/lib/python3/dist-packages/pip/_internal/cli/main.py", line 73, in main
command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/pip/_internal/commands/__init__.py", line 96, in create_command
module = importlib.import_module(module_path)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/usr/lib/python3/dist-packages/pip/_internal/commands/install.py", line 24, in <module>
from pip._internal.cli.req_command import RequirementCommand
File "/usr/lib/python3/dist-packages/pip/_internal/cli/req_command.py", line 15, in <module>
from pip._internal.index.package_finder import PackageFinder
File "/usr/lib/python3/dist-packages/pip/_internal/index/package_finder.py", line 21, in <module>
from pip._internal.index.collector import parse_links
File "/usr/lib/python3/dist-packages/pip/_internal/index/collector.py", line 12, in <module>
from pip._vendor import html5lib, requests
ImportError: cannot import name 'html5lib' from 'pip._vendor' (/usr/lib/python3/dist-packages/pip/_vendor/__init__.py)
해보니까, 그냥 "list" 옵션으로 실행해도 동일한 오류가 발생합니다.
$ python3.11 -m pip list
...[생략]...
ImportError: cannot import name 'html5lib' from 'pip._vendor' (/usr/lib/python3/dist-packages/pip/_vendor/__init__.py)
다음과 같이 버전 확인을 해보면,
$ python3.11 -V
Python 3.11.0
$ python3.11 -m pip -V
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.11)
pip의 경우 마지막에 "(python 3.11)"이라고 나오지만 왠지 그 앞의 경로가 ".../python3/..."인 것이 찝찝합니다. 그래서 검색해 보니 ^^ 해결책이 나옵니다.
ImportError: cannot import name 'html5lib' from 'pip._vendor' (/usr/lib/python3/dist-packages/pip/_vendor/__init__.py) [duplicate]
; https://stackoverflow.com/questions/70431655/importerror-cannot-import-name-html5lib-from-pip-vendor-usr-lib-python3
결국 python 3.11만의 pip 설치가 없었던 것입니다. 그래서 별도로 설치해 주면,
$ curl -sS https://bootstrap.pypa.io/get-pip.py | python3.11
Defaulting to user installation because normal site-packages is not writeable
Collecting pip
Downloading pip-22.3.1-py3-none-any.whl (2.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.1/2.1 MB 8.8 MB/s eta 0:00:00
Installing collected packages: pip
Successfully installed pip-22.3.1
$ python3.11 -m pip -V
pip 22.3.1 from /home/testusr/.local/lib/python3.11/site-packages/pip (python 3.11)
이후 pip 명령어가 아주 잘 동작합니다. ^^
$ python3.11 -m pip list
...[생략]...
wheel 0.34.2
zipp 1.0.0
zope.interface 4.7.1
참고로 Flask-Migrate의 경우 의존하는 Flask-SQLAlchemy에 버전 지정이 안 되어 있어 항상 최신 버전을 설치하게 됩니다. 그래서 (2013년에 릴리스된) "pip install Flask-Migrate==0.1.0"를 설치해도 3.0.2 버전의 Flask-SQLAlchemy가 설치되고, 그것은 다시 Flask를 2.2.2 버전을 요구해 Flask를 교체해 버립니다. 그뿐만 아니라 Flask-SQLAlchemy는 역시 버전 지정이 안 되어 있는 듯한데, SQLAlchemy를 최신의 1.4.44 버전으로 설치를 하는데요, 그런 경우 기존의 소스 코드 중 paginate 호출에서 오류가 발생합니다. ^^;
[2022-11-25 15:08:38 +0900] [2867] [ERROR] Error handling request /question/list/
Traceback (most recent call last):
File "/home/testusr/.local/lib/python3.11/site-packages/gunicorn/workers/sync.py", line 136, in handle
self.handle_request(listener, req, client, addr)
File "/home/testusr/.local/lib/python3.11/site-packages/gunicorn/workers/sync.py", line 179, in handle_request
respiter = self.wsgi(environ, resp.start_response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/testusr/.local/lib/python3.11/site-packages/flask/app.py", line 2548, in __call__
return self.wsgi_app(environ, start_response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/testusr/.local/lib/python3.11/site-packages/flask/app.py", line 2528, in wsgi_app
response = self.handle_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/testusr/.local/lib/python3.11/site-packages/flask/app.py", line 2525, in wsgi_app
response = self.full_dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/testusr/.local/lib/python3.11/site-packages/flask/app.py", line 1822, in full_dispatch_request
rv = self.handle_user_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/testusr/.local/lib/python3.11/site-packages/flask/app.py", line 1820, in full_dispatch_request
rv = self.dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/testusr/.local/lib/python3.11/site-packages/flask/app.py", line 1796, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/testprj/latest_flask/test/views/question_views.py", line 16, in _list
question_list = question_list.paginate(page, per_page=10)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Query.paginate() takes 1 positional argument but 2 positional arguments (and 1 keyword-only argument) were given
어쩔 수 없습니다, 해당 코드를 원하는 대로 수정해 해결해야 합니다.
# question_list = question_list.paginate(page, per_page=10)
question_list = question_list.paginate(page=page, per_page=10)
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]