Skip to content

ImportError: cannot import name 'BaseCommitizen' and others in files starting with cz_ #287

Closed

Description

@andres-fr

Description

As it can be seen in the following line, all files present in the Python environment whose filename begins with cz_ will be discovered and iteratively loaded by commitizen. Unfortunately this also includes files that aren't in the scope of commitizen but happen to be in PYTHONPATH:

if name.startswith("cz_")

The reaction is very cryptic and can lead to long and frustrating debugging sessions. Fortunately it is known that the "cannot import" message is linked to circular dependencies, e.g. see https://stackoverflow.com/a/746655

Just importing commitizen doesn't break, but further recursive imports down the package trigger the issue.

Steps to reproduce

  1. Create a file starting with cz_... and call e.g. from commitizen.commands.bump import Bump
  2. Run that file with the python interpreter, making sure that the file's directory is in sys.path
  3. The import will trigger an ImportError that is internal to commitizen like the one in the title.

Current behavior

Code that runs from interactive CLI doesn't run from script

Desired behavior

Code that runs on CLI should also run on script.

Environment

python --version Python 3.6.12 :: Anaconda, Inc.
python3 -c "import platform; print(platform.system())"

  • commitizen version: 2.4.2
  • python version: 3.6
  • operating system: Ubuntu

Activity

  1. andres-fr commented on Oct 27, 2020

    @andres-fr
    Author

    I guess the main idea here is that a library shouldn't discover and load files from the python path by regex. That can be very tricky to find and could be even more problematic at other levels e.g. security. At the moment I'm kind of saturated and don't have any meaningful quickfix, apart from the obvious Don't ever name any Python file starting by cz_

  2. woile commented on Oct 27, 2020

    @woile
    Member

    Sorry if it's a big frustrating 😞 , the plugin system can be improved, but it's based on a simple version of flask extension system.

    There should be a way to avoid this kind of errors, I'm open to enhancements

  3. andres-fr commented on Oct 27, 2020

    @andres-fr
    Author

    Thanks for your answer, I would have to analyze more the plugin system to propose something more specific.
    What do you think about this plugin architecture?

    https://amir.rachum.com/blog/2017/07/28/python-entry-points/

  4. Lee-W commented on Oct 28, 2020

    @Lee-W
    Member

    I browse through the article you shared but not yet try it, but it looks like a good approach to me. Also, I just found how poetry can support similiar functionality. https://python-poetry.org/docs/pyproject/#plugins

  5. woile commented on Oct 28, 2020

    @woile
    Member

    Interesting, I like it, seems quite intuitive. We'd have to update the template as well. Keep in mind, this would introduce a breaking change, but it's fine by me.

  6. andres-fr commented on Oct 29, 2020

    @andres-fr
    Author

    If you guys are comfortable with that I could give a hand in the next few weeks (we could do a hackathon or something) and try to refactor the CLI/plugin section. Only changes that you approve of course. I'd love to play around with poetry, seems neat

  7. Lee-W commented on Oct 30, 2020

    @Lee-W
    Member

    It sounds good to me. I just add a 3.0.0 tag to this issue.

  8. woile commented on Oct 30, 2020

    @woile
    Member

    I'm in, great idea! Let's coordinate a date

  9. woile commented on Nov 11, 2020

    @woile
    Member

    21 November 2020 I'll be joining to pycon argentina with a sprint for commitizen, hope you can join!

  10. andres-fr commented on Nov 11, 2020

    @andres-fr
    Author

    Sounds good, I can't promise it but will mark the date 📝. In any case this sounds amazing for the project! Looking forward to what comes out of it

  11. Lee-W commented on Mar 13, 2022

    @Lee-W
    Member

    Hi @andres-fr , https://github.com/commitizen-tools/commitizen/pull/480/files seems to solve this issue. Could you please check again? As this issue also proposed something worth enhancing, @woile do you think we should keep this one open or create a new one for tracking that stuff?

  12. woile commented on Mar 13, 2022

    @woile
    Member

    I think we should create a new ticket, I still want to refactor the plugin system

  13. andres-fr commented on Mar 13, 2022

    @andres-fr
    Author

    Hey, sorry for dropping the ball here. I'm happy to close this one if you think that is appropriate!

  14. Lee-W commented on Mar 29, 2022

    @Lee-W
    Member

    Hi all, I've created a new issue for tracking this #495. Let's close this one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions