19

I've read the other threads on this but they haven't really helped me.

I have to 2 .py files, both located under ets.routes, called agreements.py and approvals.py. enter image description here

The file agreements.py imports several methods from approvals:

from ets.routes.approvals import getPendingApprovals, getIsApprover 

It itself also exposes a utility method which should be available to approvals called authorize_agreement.

Now in approvals.py if I do

from ets.routes.agreements import authorize_agreement 

I get the error

ImportError: cannot import name 'getPendingApprovals' from partially initialized module 'ets.routes.approvals' (most likely due to a circular import) (C:\gitForVS\app\api\ets\routes\approvals.py) 

I can't move authorize_agreement to some external file like utils.py, it really should be in agreements.py because it uses a lot of DB and associated Agreement-level code which is available there. It's just that this function should be imported by its sibling, while it itself imports some of the sibling's functions. Why is that such an issue? Are you required to have 1-way imports (e.g. from approvals -> agreements only) in Python?

5
  • This error usually means you have circular imports. Commented Jun 22, 2022 at 15:14
  • In fact, the code samples show this is a circular import. Commented Jun 22, 2022 at 15:15
  • You can make it a "conditional" import. If, for example, you only need authorize_agreement inside a specific function in the approvals.py module, put the import line inside that function. The import will then not be executed when the package or module is imported, but only by the time the function is executed, avoiding the circular import. Commented Jun 22, 2022 at 15:16
  • About your question "Are you required to have 1-way imports (e.g. from approvals -> agreements only) in Python?": think about what it means, when module A imports from module B, and module B imports form module A. You get a chicken and egg problem. Draw a few diagrams with your imports and see if you can solve it: you'll probably find you run into problems. Commented Jun 22, 2022 at 15:18
  • 1
    Not really, other languages allow it, like Java. Commented Jun 22, 2022 at 15:20

2 Answers 2

23

The reason you're getting an error is because agreements.py can't import functions from approvals.py while approvals.py is simultaneously trying to import functions from agreements.py (hence the circular import error). One of your modules needs to define the functions the other wants before importing the functions it wants from the other.

For example:

File A:

from fileb import sum2 def sum(a, b): return a + b 

File B:

def sum2(a, b): return a + b + a from filea import sum # This works 
Sign up to request clarification or add additional context in comments.

1 Comment

Are there any corner cases? Or, a chance of getting an error due to race condition or anything?
23

The reason I was encountering this error was technically explained by the answer on this page from @caleb-keller although not explicitly, but turned out to have a very simple explanation:

The file name was the same as the library I was trying to import.

My example: I created elevenlabs.py to ensure my API key was working and to try out some of the library functions.

The file started with from elevenlabs import clone, generate [...] but since the file itself was named elevenlabs.py, it tried to import from itself which of course won't work.

2 Comments

Thank you, that was my problem, my script had the same name as the package. I don’t think I would have found the issue without this!
Thank you. This was the cause of the error for me

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.