{"id":44476,"date":"2023-10-04T12:33:21","date_gmt":"2023-10-04T12:33:21","guid":{"rendered":"https:\/\/ubuntuhandbook.org\/?p=44476"},"modified":"2023-10-04T12:33:21","modified_gmt":"2023-10-04T12:33:21","slug":"fix-broken-pip-python-312-ubuntu","status":"publish","type":"post","link":"https:\/\/ubuntuhandbook.org\/index.php\/2023\/10\/fix-broken-pip-python-312-ubuntu\/","title":{"rendered":"Fix Broken Pip for Python 3.12 in Ubuntu 22.04\/20.04"},"content":{"rendered":"<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python-feature-icon.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-thumbnail wp-image-34796\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python-feature-icon-250x250.png\" alt=\"\" width=\"250\" height=\"250\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python-feature-icon-250x250.png 250w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python-feature-icon-300x300.png 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python-feature-icon-600x600.png 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python-feature-icon-768x768.png 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/10\/python-feature-icon.png 1200w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/a><\/p>\n<p>This simple tutorial shows how to fix broken pip installer after installing Python 3.12 from <a href=\"https:\/\/launchpad.net\/~deadsnakes\/+archive\/ubuntu\/ppa\" target=\"_blank\" rel=\"noopener\">Deadsnakes PPA<\/a> in Ubuntu 22.04 and Ubuntu 20.04.<\/p>\n<p>Due to removal of long deprecated <code>pkgutil.ImpImporter<\/code> class, pip command may not work for Python 3.12 in your Linux with old setuptools. It either just does not work or outputs following error, when installing anything via <code>pip install<\/code>:<\/p>\n<pre>ERROR: Exception:\r\nTraceback (most recent call last):\r\n  File \"\/usr\/lib\/python3\/dist-packages\/pip\/_internal\/cli\/base_command.py\", line 165, in exc_logging_wrapper\r\n    status = run_func(*args)\r\n             ^^^^^^^^^^^^^^^\r\n  File \"\/usr\/lib\/python3\/dist-packages\/pip\/_internal\/cli\/req_command.py\", line 205, in wrapper\r\n    return func(self, options, args)\r\n           ^^^^^^^^^^^^^^^^^^^^^^^^^\r\n  File \"\/usr\/lib\/python3\/dist-packages\/pip\/_internal\/commands\/install.py\", line 285, in run\r\n    session = self.get_default_session(options)\r\n              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n  File \"\/usr\/lib\/python3\/dist-packages\/pip\/_internal\/cli\/req_command.py\", line 75, in get_default_session\r\n    self._session = self.enter_context(self._build_session(options))\r\n                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n  File \"\/usr\/lib\/python3\/dist-packages\/pip\/_internal\/cli\/req_command.py\", line 89, in _build_session\r\n    session = PipSession(\r\n              ^^^^^^^^^^^\r\n  File \"\/usr\/lib\/python3\/dist-packages\/pip\/_internal\/network\/session.py\", line 282, in __init__\r\n    self.headers[\"User-Agent\"] = user_agent()\r\n                                 ^^^^^^^^^^^^\r\n  File \"\/usr\/lib\/python3\/dist-packages\/pip\/_internal\/network\/session.py\", line 157, in user_agent\r\n    setuptools_dist = get_default_environment().get_distribution(\"setuptools\")\r\n                      ^^^^^^^^^^^^^^^^^^^^^^^^^\r\n  File \"\/usr\/lib\/python3\/dist-packages\/pip\/_internal\/metadata\/__init__.py\", line 24, in get_default_environment\r\n    from .pkg_resources import Environment\r\n  File \"\/usr\/lib\/python3\/dist-packages\/pip\/_internal\/metadata\/pkg_resources.py\", line 9, in \r\n    from pip._vendor import pkg_resources\r\n  File \"\/usr\/lib\/python3\/dist-packages\/pip\/_vendor\/pkg_resources\/__init__.py\", line 2164, in \r\n    register_finder(pkgutil.ImpImporter, find_on_path)\r\n                    ^^^^^^^^^^^^^^^^^^^\r\n<b>AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?<\/b><\/pre>\n<p>To fix the issue, you just have to manually install pip for Python 3.12. And, here&#8217;s how to do the job in 2 ways.<\/p>\n<h3>Method 1: Install pip via ensurepip<\/h3>\n<p>Python has a built-in module <code>ensurepip<\/code>, to provide support for bootstrapping the pip installer into an existing Python installation.<\/p>\n<p>First, press <b>Ctrl+Alt+T<\/b> on keyboard to open terminal. When it opens, run command:<\/p>\n<pre>python3.12 -m ensurepip --upgrade<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/10\/ensurepip-312.webp\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/10\/ensurepip-312-600x259.webp\" alt=\"\" width=\"600\" height=\"259\" class=\"aligncenter size-large wp-image-44477\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/10\/ensurepip-312-600x259.webp 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/10\/ensurepip-312-300x129.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/10\/ensurepip-312-768x331.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/10\/ensurepip-312.webp 786w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>After that, you should be able to use the pip installer for Python 3.12 by either <code>pip3.12<\/code> or <code>python3.12 -m pip<\/code> command.<\/p>\n<h3>Method 2: Install pip via get-pip.py<\/h3>\n<p>In case the previous method does not work for you, <a href=\"https:\/\/pypa.io\/\" rel=\"noopener\" target=\"_blank\">Pypa.io<\/a> provides a script that can do the same job.<\/p>\n<p>First, open terminal (Ctrl+Alt+T) and run command to download the script via wget command:<\/p>\n<pre>wget https:\/\/bootstrap.pypa.io\/get-pip.py<\/pre>\n<p>Then, just run the script to download &#038; install pip:<\/p>\n<pre>python3.12 get-pip.py<\/pre>\n<p>After installation, you may run <code>python3.12 -m pip<\/code> command to use the pip installer. To bind it to <code>pip3.12<\/code>, just run <code>alias pip3.12=\"python3.12 -m pip\"<\/code>.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/10\/get-pip.webp\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/10\/get-pip-600x407.webp\" alt=\"\" width=\"600\" height=\"407\" class=\"aligncenter size-large wp-image-44478\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/10\/get-pip-600x407.webp 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/10\/get-pip-300x204.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/10\/get-pip-768x521.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2023\/10\/get-pip.webp 850w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>That&#8217;s all. Enjoy!<\/p>","protected":false},"excerpt":{"rendered":"<p>This simple tutorial shows how to fix broken pip installer after installing Python 3.12 from Deadsnakes PPA in Ubuntu 22.04 and Ubuntu 20.04. Due to removal of long deprecated pkgutil.ImpImporter class, pip command may not work for Python 3.12 in your Linux with old setuptools. It either just does not work or outputs following error, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":34796,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[2045],"class_list":["post-44476","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","tag-python"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/44476","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/comments?post=44476"}],"version-history":[{"count":0,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/44476\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media\/34796"}],"wp:attachment":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media?parent=44476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/categories?post=44476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/tags?post=44476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}