apache / superset

Apache Superset is a Data Visualization and Data Exploration Platform
https://superset.apache.org/
Apache License 2.0
61.62k stars 13.46k forks source link

No module named 'superset.translations.utils' #29613

Open praveer38 opened 1 month ago

praveer38 commented 1 month ago

Bug description

Traceback (most recent call last): File "/usr/local/bin/superset", line 33, in sys.exit(load_entry_point('apache-superset', 'console_scripts', 'superset')()) File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1130, in call return self.main(args, kwargs) File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1055, in main rv = self.invoke(ctx) File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1654, in invoke super().invoke(ctx) File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1404, in invoke return ctx.invoke(self.callback, ctx.params) File "/usr/local/lib/python3.9/site-packages/click/core.py", line 760, in invoke return __callback(args, kwargs) File "/usr/local/lib/python3.9/site-packages/click/decorators.py", line 26, in new_func return f(get_current_context(), *args, *kwargs) File "/usr/local/lib/python3.9/site-packages/flask/cli.py", line 354, in decorator app = __ctx.ensure_object(ScriptInfo).load_app() File "/usr/local/lib/python3.9/site-packages/flask/cli.py", line 308, in load_app app = locate_app(import_name, name) File "/usr/local/lib/python3.9/site-packages/flask/cli.py", line 237, in locate_app return find_app_by_string(module, app_name) File "/usr/local/lib/python3.9/site-packages/flask/cli.py", line 165, in find_app_by_string app = attr(args, kwargs) File "/app/superset/app.py", line 47, in create_app raise ex File "/app/superset/app.py", line 40, in create_app app_initializer.init_app() File "/app/superset/initialization/init.py", line 489, in init_app self.init_app_in_ctx() File "/app/superset/initialization/init.py", line 417, in init_app_in_ctx self.configure_data_sources() File "/app/superset/initialization/init.py", line 515, in configure_data_sources import(module_name, fromlist=class_names) File "/app/superset/connectors/sqla/init.py", line 17, in from . import models, views File "/app/superset/connectors/sqla/views.py", line 35, in from superset.views.base import ( File "/app/superset/views/init.py", line 17, in from . import ( File "/app/superset/views/alerts.py", line 25, in from .base import BaseSupersetView File "/app/superset/views/base.py", line 81, in from superset.translations.utils import get_language_pack ModuleNotFoundError: No module named 'superset.translations.utils'

How to reproduce the bug

This is happening after creating the container using the dockerfile created image.

Screenshots/recordings

No response

Superset version

4.0.2

Python version

3.9

Node version

16

Browser

Chrome

Additional context

No response

Checklist

dosubot[bot] commented 1 month ago

@praveer38 Hello there! I'm here to help you with any bugs, questions, or contributions you have for the repository. Let me know how I can assist you!

To resolve the ModuleNotFoundError: No module named 'superset.translations.utils' error, ensure that the superset/translations directory is correctly created and populated. Here are the steps to follow:

  1. Ensure the babel-compile Command Runs Successfully: Make sure the following command in your Dockerfile runs without errors:

    RUN flask fab babel-compile --target superset/translations
  2. Check the PYTHONPATH Environment Variable: Ensure the PYTHONPATH includes the path to the superset directory:

    ENV PYTHONPATH=/home/superset/superset/:$PYTHONPATH
  3. Copy and Compile Translations: Ensure your Dockerfile includes the steps to copy and compile translations:

    # This copies the .po files needed for translation
    RUN mkdir -p /app/superset/translations
    COPY superset/translations /app/superset/translations
    # Compiles .json files from the .po files, then deletes the .po files
    RUN npm run build-translation
    RUN rm /app/superset/translations/*/LC_MESSAGES/*.po
    RUN rm /app/superset/translations/messages.pot
    
    # Copy the .json translations from the frontend layer
    COPY --chown=superset:superset --from=superset-node /app/superset/translations superset/translations
    
    # Compile translations for the backend - this generates .mo files, then deletes the .po files
    COPY ./scripts/translations/generate_mo_files.sh ./scripts/translations/
    RUN ./scripts/translations/generate_mo_files.sh \
       && chown -R superset:superset superset/translations \
       && rm superset/translations/messages.pot \
       && rm superset/translations/*/LC_MESSAGES/*.po
  4. Verify the utils.py File: Ensure the superset/translations/utils.py file exists and is correctly referenced. Here is a snippet of what the utils.py file might look like:

    import json
    import logging
    import os
    from typing import Any, Optional
    
    logger = logging.getLogger(__name__)
    
    # Global caching for JSON language packs
    ALL_LANGUAGE_PACKS: dict[str, dict[str, Any]] = {"en": {}}
    
    DIR = os.path.dirname(os.path.abspath(__file__))
    
    def get_language_pack(locale: str) -> Optional[dict[str, Any]]:
       """Get/cache a language pack
    
       Returns the language pack from cache if it exists, caches otherwise
    
       >>> get_language_pack('fr')['Dashboards']
       "Tableaux de bords"
       """
       pack = ALL_LANGUAGE_PACKS.get(locale)
       if not pack:
           filename = DIR + f"/{locale}/LC_MESSAGES/messages.json"
           if not locale or locale == "en":
               # Forcing a dummy, quasy-empty language pack for English since the file
               # in the en directory is contains data with empty mappings
               filename = DIR + "/empty_language_pack.json"
           try:
               with open(filename, encoding="utf8") as f:
                   pack = json.load(f)
                   ALL_LANGUAGE_PACKS[locale] = pack or {}
           except Exception:  # pylint: disable=broad-except
               logger.error(
                   "Error loading language pack for, falling back on en %s", locale
               )
               pack = get_language_pack("en")
       return pack
  5. Check for Recent Documentation Updates: Starting with version 4.0.0, translations are now bundled inside the Python package. Ensure you are following the updated guidelines:

    - [28483](https://github.com/apache/superset/pull/28483) Starting with this version we bundle
     translations inside the python package. This includes the .mo files needed by pybabel on the
     backend, as well as the .json files used by the frontend. If you were doing anything before
     as part of your bundling to expose translation packages, it's probably not needed anymore.

By following these steps, you should be able to resolve the ModuleNotFoundError related to superset.translations.utils [1][2][3][4][5].

To continue talking to Dosu, mention @dosu.

praveer38 commented 1 month ago

@dosu "scripts/translations/generate_mo_files.sh: no such file or directory" : I'm getting this error while trying to compile translations using your step 3.

dosubot[bot] commented 1 month ago

To resolve the scripts/translations/generate_mo_files.sh: no such file or directory error and ensure the superset/translations issue is fixed, you need to update your Dockerfile to include the necessary steps for handling translation files. Here is an updated version of your Dockerfile:

FROM apache/superset:4.0.2

USER root

# Install additional dependencies
RUN pip install mysqlclient
RUN pip install Authlib

# Copy initialization scripts
COPY ./docker-init.sh /superset-init.sh
COPY docker-bootstrap.sh /app/docker/docker-bootstrap.sh

# Copy Superset configuration
COPY superset_config.py /app/
ENV SUPERSET_CONFIG_PATH /app/superset_config.py

# Ensure the translations directory exists and compile translations
RUN mkdir -p /app/superset/translations
COPY superset/translations /app/superset/translations

# Compile translations for the backend
COPY ./scripts/translations/generate_mo_files.sh ./scripts/translations/
RUN ./scripts/translations/generate_mo_files.sh \
    && chown -R superset:superset /app/superset/translations \
    && rm /app/superset/translations/messages.pot \
    && rm /app/superset/translations/*/LC_MESSAGES/*.po

USER superset

ENTRYPOINT [ "/superset-init.sh" ]

Ensure that the generate_mo_files.sh script is present in the ./scripts/translations/ directory and contains the necessary commands to compile the .mo files. Here is an example of what the generate_mo_files.sh script might look like:

#!/bin/bash

# Compile .po files to .mo files
for lang in $(ls /app/superset/translations); do
    msgfmt /app/superset/translations/$lang/LC_MESSAGES/messages.po -o /app/superset/translations/$lang/LC_MESSAGES/messages.mo
done

By including these steps, you should be able to resolve the ModuleNotFoundError and ensure that the translation files are correctly compiled and available [1][2].

To continue talking to Dosu, mention @dosu.