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).

1 Like

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

Build from source definitely takes longer tho

I met the same problem: when I install python-3.13.2 on AlmaLinux 9.5 OS:

Traceback (most recent call last):
File “”, line 601, in _get_decompress_func
ModuleNotFoundError: No module named ‘zlib’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “”, line 649, in _get_data
File “”, line 604, 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 “”, line 6, in
runpy.run_module(“pip”, run_name=“main”, alter_sys=True)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “”, line 222, in run_module
File “”, line 148, in _get_module_details
File “”, line 112, in _get_module_details
File “”, line 137, in get_code
File “”, line 783, in _get_module_code
File “”, line 651, in _get_data
zipimport.ZipImportError: can’t decompress data; zlib not available
Traceback (most recent call last):
File “”, line 198, in _run_module_as_main
File “”, line 88, in _run_code
File “/home/tony/dev-env/Python-3.13.2/Lib/ensurepip/main.py”, line 5, in
sys.exit(ensurepip._main())
~~~~~~~~~~~~~~~^^
File “/home/tony/dev-env/Python-3.13.2/Lib/ensurepip/init.py”, line 257, in _main
return _bootstrap(
root=args.root,
…<4 lines>…
default_pip=args.default_pip,
)
File “/home/tony/dev-env/Python-3.13.2/Lib/ensurepip/init.py”, line 172, in _bootstrap
return _run_pip([*args, “pip”], [os.fsdecode(tmp_wheel_path)])
File “/home/tony/dev-env/Python-3.13.2/Lib/ensurepip/init.py”, line 87, in _run_pip
return subprocess.run(cmd, check=True).returncode
~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
File “/home/tony/dev-env/Python-3.13.2/Lib/subprocess.py”, line 579, in run
raise CalledProcessError(retcode, process.args,
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command ‘[’/home/tony/dev-env/Python-3.13.2/python’, ‘-W’, ‘ignore::DeprecationWarning’, ‘-c’, ‘\nimport runpy\nimport sys\nsys.path = ['/tmp/tmpl26j29kl/pip-24.3.1-py3-none-any.whl'] + sys.path\nsys.argv[1:] = ['install', '–no-cache-dir', '–no-index', '–find-links', '/tmp/tmpl26j29kl', '–root', '/', '–upgrade', 'pip']\nrunpy.run_module(“pip”, run_name=“main”, alter_sys=True)\n’]’ returned non-zero exit status 1.
make: *** [Makefile:2249: install] Error 1
[tony@localhost Python-3.13.2]$

===============================

Then, I tried this :

sudo yum install python3-pip

and then redo:
./configure
make
make install
,
but it still doesn’t work

Please help me ! Thank you.

it does not work.

Almost certainly you are missing important devel packages.
Check the output of ./configure for messages about failing to find packages. I would guess you are missing zlib devel package as a minimum.

Have you check to see if python you want is package in EPEL?
It seems to have a build for 3.13 python3.13-3.13.2-2.eln146 | Build Info | koji