Skip to content

option --init-missing of pybabel update should also create the corresponding directory #1139

Closed

Description

@du33169

I use the option --init-missing of pybabel update to avoid redundant pybabel init command. However, this option only creates the *.po file, and will fail if the target directory ([locale]/LC_MESSAGES) does not exist yet:

creating catalog .\en_US\LC_MESSAGES\strings.po based on strings.pot
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "...\.venv\Scripts\pybabel.exe\__main__.py", line 8, in <module>
  File "...\.venv\Lib\site-packages\babel\messages\frontend.py", line 999, in main
    return CommandLineInterface().run(sys.argv)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\.venv\Lib\site-packages\babel\messages\frontend.py", line 925, in run
    return cmdinst.run()
           ^^^^^^^^^^^^^
  File "...\.venv\Lib\site-packages\babel\messages\frontend.py", line 795, in run
    with open(filename, 'wb') as outfile:
         ^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '.\\en_US\\LC_MESSAGES\\strings.po'

This behavior makes the option less useful. I suggest invoke os.makedirs in UpdateCatalog.finalize_options, just like in InitCatalog.finalize_options:

if not os.path.exists(os.path.dirname(self.output_file)):
os.makedirs(os.path.dirname(self.output_file))

but with extra exist_ok=True argument, as the directory may already exist for the update command.

Or it might be better to reuse the logic in InitCatalog.

Activity

  1. akx commented on Oct 19, 2024

    @akx
    Member

    Makes sense! If you feel like writing a PR, that would be appreciated.

  2. du33169 commented on Oct 22, 2024

    @du33169
    ContributorAuthor

    I'll look into it, but it will be a few days before I'm free.
    If you or anyone else is interested and can nail it, please go ahead.

  3. akx commented on Jan 13, 2025

    @akx
    Member

    Hmm – I'm actually unable to repro this 🤔

    ~/b/babel $ pybabel update --init-missing -i tests/messages/data/project/i18n/messages.pot -d $(mktemp -d) -l en_US
    creating catalog /var/folders/nz/6mg9yzdj1kq8lg2tsc5y9rlc0000gn/T/tmp.gNcQgcfVyp/en_US/LC_MESSAGES/messages.po based on tests/messages/data/project/i18n/messages.pot
    updating catalog /var/folders/nz/6mg9yzdj1kq8lg2tsc5y9rlc0000gn/T/tmp.gNcQgcfVyp/en_US/LC_MESSAGES/messages.po based on tests/messages/data/project/i18n/messages.pot
    
  4. du33169 commented on Jan 13, 2025

    @du33169
    ContributorAuthor

    It still occurs for me with Babel v2.16.0 and Python 3.11.
    Here's what I did:

    #git clone https://github.com/python-babel/babel.git
    mkdir test_babel
    cd test_babel
    cp ../babel/tests/messages/data/project/i18n/messages.pot ./
    python -m venv .venv
    source .venv/bin/activate
    pip install babel
    pybabel update --init-missing -i messages.pot -d $(mktemp -d) -l en_US

    And the output:
    log.txt

  5. self-assigned this
    on Jan 13, 2025
  6. added 2 commits that reference this issue on Jan 7, 2026
    9826fc5
    ff8071e
  7. added a commit that references this issue on Jan 10, 2026
    851db43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions