ModuleNotFoundError: No module named 'zlib'

I INSTALLED zlib1g-dev!!!

I am trying to install python3.11 on wsl2, the python installed successfully but I can’t install pip for it while running python3.11 -m ensurepip. The first error I got is the title. Full Traceback below:

Traceback (most recent call last):
  File "<frozen zipimport>", line 576, in _get_decompress_func
ModuleNotFoundError: No module named 'zlib'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen zipimport>", line 624, in _get_data
  File "<frozen zipimport>", line 579, in _get_decompress_func
zipimport.ZipImportError: can't decompress data; zlib not available

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 6, in <module>
  File "<frozen runpy>", line 222, in run_module
  File "<frozen runpy>", line 148, in _get_module_details
  File "<frozen runpy>", line 112, in _get_module_details
  File "<frozen zipimport>", line 195, in get_code
  File "<frozen zipimport>", line 758, in _get_module_code
  File "<frozen zipimport>", line 626, in _get_data
zipimport.ZipImportError: can't decompress data; zlib not available
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/local/lib/python3.11/ensurepip/__main__.py", line 5, in <module>
    sys.exit(ensurepip._main())
             ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/ensurepip/__init__.py", line 286, in _main
    return _bootstrap(
           ^^^^^^^^^^^
  File "/usr/local/lib/python3.11/ensurepip/__init__.py", line 202, in _bootstrap
    return _run_pip([*args, *_PACKAGE_NAMES], additional_paths)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/ensurepip/__init__.py", line 103, in _run_pip
    return subprocess.run(cmd, check=True).returncode
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/subprocess.py", line 571, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['/usr/local/bin/python3.11', '-W', 'ignore::DeprecationWarning', '-c', '\nimport runpy\nimport sys\nsys.path = [\'/tmp/tmpr8vecyad/setuptools-65.5.0-py3-none-any.whl\', \'/tmp/tmpr8vecyad/pip-22.3.1-py3-none-any.whl\'] + sys.path\nsys.argv[1:] = [\'install\', \'--no-cache-dir\', \'--no-index\', \'--find-links\', \'/tmp/tmpr8vecyad\', \'setuptools\', \'pip\']\nrunpy.run_module("pip", run_name="__main__", alter_sys=True)\n']' returned non-zero exit status 1.

I installed the Python from deadsnakes ppa. If it helps, I installed another python3.11.0rc1 from apt official source and update it after I added the PPA. I don’t understand what is going on, everyone on the internet said to install zlib1g but I already did before I installed Python, and run the command dozens of times.

Install pip using apt - I think like this:

apt install python3-pip

I suppose that will install pip for the system python? I am trying to use python3.11 as an alternative version instead of overwriting the default python version, which I think is recommended.

Yes, that makes a lot of difference, and you may need to talk to whoever manages that PPA to get help. We’re not really able to help. This is probably a configuration issue with the deadsnakes distribution.

My recommendation? Build Python from source instead. It’s not that hard, and will give you the absolute latest version (or any older version as you choose).

okay, i just thought deadsnakes is maintained by the official team

Build from source definitely takes longer tho