Skip to content

Error when using write hooks in python < 3.9 #934

Description

@CaselIT

Hi, I have an issue which boils down to the same point in alembic code, but I'm not using any development-only script, but [post_write_hooks] feature.

Relevant part of alembic.ini:

[post_write_hooks]

# format using "black" - use the console_scripts runner, against the "black" entrypoint
hooks = black, isort
black.type = console_scripts
black.entrypoint = black
black.options = REVISION_SCRIPT_FILENAME

isort.type = console_scripts
isort.entrypoint = isort
isort.options = REVISION_SCRIPT_FILENAME
16:18 $ poetry run alembic revision --autogenerate -m "Add foobar"
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
  Generating /home/foobar/myproject/api/migrations/versions/2021_09_27_6c36df7b6df9_add_foobar.py ...  done
  Running post write hook "black" ...
  FAILED
Traceback (most recent call last):
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/bin/alembic", line 8, in <module>
    sys.exit(main())
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/config.py", line 588, in main
    CommandLine(prog=prog).main(argv=argv)
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/config.py", line 582, in main
    self.run_cmd(cfg, options)
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/config.py", line 559, in run_cmd
    fn(
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/command.py", line 234, in revision
    scripts = [script for script in revision_context.generate_scripts()]
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/command.py", line 234, in <listcomp>
    scripts = [script for script in revision_context.generate_scripts()]
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/autogenerate/api.py", line 600, in generate_scripts
    yield self._to_script(generated_revision)
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/autogenerate/api.py", line 505, in _to_script
    return self.script_directory.generate_revision(
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/script/base.py", line 736, in generate_revision
    write_hooks._run_hooks(path, post_write_hooks)
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/script/write_hooks.py", line 84, in _run_hooks
    util.status(
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/util/messaging.py", line 56, in status
    ret = fn(*arg, **kw)
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/script/write_hooks.py", line 58, in _invoke
    return hook(revision, options)
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/script/write_hooks.py", line 145, in console_scripts
    "import %s; %s.%s()" % (impl.module, impl.module, impl.attr),
AttributeError: 'EntryPoint' object has no attribute 'module'

The origin of the issue is of course the same, alembic/script/write_hooks.py seems to be working with python 3.9+ as discovered above:

This happens, because the code assumes (write_hooks.py:141), that the returned object EntryPoint has a module or attr property (which it has on 3.9, but not on 3.8).

While write_pyi.py is an script for development use only, I guess that all post-write hooks deal with the same issue as well?

Originally posted by @antoni-szych-rtbhouse in #915 (comment)

Activity

  1. CaselIT commented on Sep 30, 2021

    @CaselIT
    MemberAuthor

    The issue can be solved by installing importlib_metadata also on 3.8.

    I wonder why the test did not catch it. I guess we need to add a covering case for this

  2. jmrodriguez-lab commented on Sep 30, 2021

    @jmrodriguez-lab

    I'm on Python 3.8.8 with alembic==1.7.3 and installing importlib_metadata does not solve the issue.
    Maybe there's something else to install? (this error happens with the black hook from the .ini example).

    Any hint would be appreciated :-)

  3. CaselIT commented on Sep 30, 2021

    @CaselIT
    MemberAuthor

    Sorry, I was not clear, a change in the code is still needed, changing this condition to if py39

    if py38:
    from importlib import metadata as importlib_metadata
    else:
    import importlib_metadata # type:ignore[no-redef] # noqa

    will try to solve today so we can hopefully release a fix

  4. sqla-tester commented on Sep 30, 2021

    @sqla-tester
    Collaborator

    Federico Caselli has proposed a fix for this issue in the master branch:

    Fix write hook issue in python < 3.9. https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/3115

  5. added a commit that references this issue on Jun 11, 2025
    8d166b1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions