mollie / mollie-api-python

Mollie API client for Python
http://www.mollie.com
BSD 2-Clause "Simplified" License
113 stars 55 forks source link

ImportError: cannot import name 'Payment' from partially initialized module #301

Closed pennersr closed 1 year ago

pennersr commented 1 year ago

When trying to import the Payment class you hit a circular import (tested with 3.0.0, 3.0.1, 3.1.0):

# python
Python 3.11.2 (main, Feb  9 2023, 00:38:19) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from mollie.api.objects.payment import Payment
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.11/site-packages/mollie/api/objects/payment.py", line 1, in <module>
    from ..resources import PaymentCaptures, PaymentChargebacks
  File "/usr/local/lib/python3.11/site-packages/mollie/api/resources/__init__.py", line 13, in <module>
    from .payments import *  # noqa: F401, F403
    ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/mollie/api/resources/payments.py", line 6, in <module>
    from ..objects.payment import Payment
ImportError: cannot import name 'Payment' from partially initialized module 'mollie.api.objects.payment' (most likely due to a circular import) (/usr/local/lib/python3.11/site-packages/mollie/api/objects/payment.py)

Workaround:

>>> # Add this dummy import
>>> import mollie.api.resources.payments
>>> # Now this works:
>>> from mollie.api.objects.payment import Payment
whyscream commented 1 year ago

This is probably due to the additional imports needed from type annotations, and the refactor. We'll try to improve the situation shortly, thanks for the notice.

whyscream commented 1 year ago

HI @pennersr, today we released 3.1.1, which resolves the circular import for Payment (and other result classes).