0

I have few common functions written in file a.py which is called from file b.py. And, also I have few common functions written in file b.py which is called from file a.py. but when I try to run these files I get error as unable to import name. Below is the code for reference.

I have just provided an example below, these files have common piece of code in real scenario which is specific to a functionality and that is the reason its being called from one another

a.py code below

from b import get_partner_id


def get_customer_id(tenant_id):
    customer_id = tenant_id + "tenant_name"
    return customer_id


def get_partner_details():
    partnerid = get_partner_id()
    data = {"partnerId": partnerid}
    print(data)

b.py code below

from a import get_customer_id


def get_partner_id():
    customer_id = get_customer_id("7687")
    env = 'sandbox-all' + customer_id
    return env


get_partner_id()

Below is the error for reference,

Traceback (most recent call last):
  File "C:/testCases/b.py", line 1, in <module>
    from a import get_customer_id
  File "C:\testCases\a.py", line 2, in <module>
    from b import get_partner_id
  File "C:\testCases\b.py", line 1, in <module>
    from a import get_customer_id
ImportError: cannot import name 'get_customer_id'
Prashanth
  • 93
  • 11
  • There are ways to work around this... but your example looks like you should not be doing this in the first place. Separating generic customer and specific partner may make some sense, but *mixing* their parts across files does not. Put all the ID stuff in one file, or put all the partner stuff in one file, or put everything in one file. – MisterMiyagi Dec 23 '21 at 08:02
  • This question has been asked and answered multiple times on SO. Please check for instance https://stackoverflow.com/questions/7336802/how-to-avoid-circular-imports-in-python And as @MisterMiyagi pointed out: This shouldn't occur in well-designed code! – C Hecht Dec 23 '21 at 08:40
  • Does this answer your question? [Circular import dependency in Python](https://stackoverflow.com/questions/1556387/circular-import-dependency-in-python) – MisterMiyagi Dec 23 '21 at 08:46

0 Answers0