pythongoogle-cloud-platformapt-getgoogle-cloud-workstations

Python 3.10.7 Import Error ModuleNotFoundError: No module named '_bz2'


I am trying to create a Google Cloud Workstation that has all of my required tooling built-in such as PHP, MYSQL and PYTHON with BZ2 support. I understand that this involves creating a custom container image. Here is the Dockerfile that I used:

FROM us-central1-docker.pkg.dev/cloud-workstations-images/predefined/code-oss:latest

RUN apt-get update && apt-get install -y \
    apache2 \
    bzip2 \
    libbz2-dev \
    php

This installs the necessary modules, as far as I know. The next thing I did was docker run --rm -it --entrypoint=bash myimage:latest to run the image locally. Then I used this command bzip2 --version to verify that bz2 has been installed.

The next thing I did was to tag the image with the name of the Google Cloud Repository: docker tag myimage gcr.io/myproject/myimage:latest and then push the image to the the repository with docker -- push gcr.io/myproject/myimage:latest

The next step was to launch a new workstation based on the custom image. When I did this I noticed that Python 3.10.7 was installed automatically without having to specify this in the Dockerfile. When I launch python and try to import the bz2 module, I receive this error:

Python 3.10.7 (main, Jan  3 2023, 22:08:44) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import bz2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/bz2.py", line 17, in <module>
    from _bz2 import BZ2Compressor, BZ2Decompressor
ModuleNotFoundError: No module named '_bz2'

What am I doing wrong?


Solution

  • As per the error ModuleNotFoundError: No module named '_bz2' it states that bz2 file or headers are missing in the python installation that's the reason this error is getting. Seems to be this error is generated for every python version for bz2 and libbz2 file missing in it. You need to build python with BZIP2 support refer to this link

    Refer to this SO and bz2 support file to update the bz2 and libbz2 file which resolve your issue.