Search code examples
pythonpyenvbzip2

Pyenv's python is missing bzip2 module


I used pyenv to install python 3.8.2 and to create a virtualenv. In the virtualenv, I used pipenv to install pandas.

However, when importing pandas, I'm getting the following:

  [...]
  File "/home/luislhl/.pyenv/versions/poc-prefect/lib/python3.8/site-packages/pandas/io/common.py", line 3, in <module>
    import bz2
  File "/home/luislhl/.pyenv/versions/3.8.2/lib/python3.8/bz2.py", line 19, in <module>
    from _bz2 import BZ2Compressor, BZ2Decompressor
ModuleNotFoundError: No module named '_bz2'

After some googling, I found out some people suggesting I rebuild Python from source after installing bzip2 library in my system.

However, after trying installing it with sudo dnf install bzip2-devel I see that I already had it installed.

As far as I know, pyenv builds python from source when installing some version. So, why wasn't it capable of including the bzip2 module when building?

How can I manage to rebuild Python using pyenv in order to make bzip2 available? I'm in Fedora 30

Thanks in advance

UPDATE I tried installing another version of python with pyenv in verbose mode, to see the compilation output.

There is this message in the end of the compilation:

WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?

But as I stated before, I checked I already have bzip2 installed in my system. So I don't know what to do.


Solution

  • On Ubuntu 22 LTS

    Missing Library Problem in Python Installation with Pyenv

    Before the fix:

    $> pyenv install 3.11.0
    

    command result:

    pyenv: /home/user/.pyenv/versions/3.11.0 already exists
    continue with installation? (y/N) y
    Downloading Python-3.11.0.tar.xz...
    -> https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tar.xz
    
    
    Installing Python-3.11.0...
    WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
    WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
    WARNING: The Python lzma extension was not compiled. Missing the lzma lib?
    
    

    TLDR;

    Recipe to fix:

    sudo apt-get install build-essential zlib1g-dev libffi-dev libssl-dev libbz2-dev libreadline-dev libsqlite3-dev liblzma-dev
    
    

    Result

    After the fix:

    $> pyenv install 3.11.0
    

    Command result:

    pyenv: /home/user/.pyenv/versions/3.11.0 already exists
    continue with installation? (y/N) y
    Downloading Python-3.11.0.tar.xz...
    -> https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tar.xz
    Installing Python-3.11.0...
    Installed Python-3.11.0 to /home/user/.pyenv/versions/3.11.0