Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
andres-fr opened this issue Oct 27, 2020 · 14 comments
Labels

Comments

@andres-fr
Copy link

andres-fr commented Oct 27, 2020

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
@andres-fr
Copy link
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_

@woile
Copy link
Member

woile commented Oct 27, 2020

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

@andres-fr
Copy link
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/

@Lee-W
Copy link
Member

Lee-W commented Oct 28, 2020

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

@woile
Copy link
Member

woile commented Oct 28, 2020

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.

@andres-fr
Copy link
Author

andres-fr commented Oct 29, 2020

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

@Lee-W
Copy link
Member

Lee-W commented Oct 30, 2020

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

@woile woile added the 3.0.0 label Oct 30, 2020
@woile
Copy link
Member

woile commented Oct 30, 2020

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

@woile
Copy link
Member

woile commented Nov 11, 2020

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

@andres-fr
Copy link
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

@Lee-W
Copy link
Member

Lee-W commented Mar 13, 2022

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?

@woile
Copy link
Member

woile commented Mar 13, 2022

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

@andres-fr
Copy link
Author

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

@Lee-W
Copy link
Member

Lee-W commented Mar 29, 2022

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

@Lee-W Lee-W closed this as completed Mar 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants