How to Solve Circular Import Error in Python

March 08, 2022


To give some context, I have two python files. (Both in same folder - jira_wrapper)

  • (have a class JiraClient)

And, I need to import one into another. I imported them as:

# file
from jira_wrapper.jira_client import JiraClient

# file
from jira_wrapper import retry_helper

I got this error:

ImportError: cannot import name 'JiraClient' from partially initialized module 'jira_wrapper.jira_client' (most likely due to a circular import) (/jira_wrapper/

I hope you get the scenario. One is dependent on Two, Two is dependent on One. Kind of a chicken-egg problem. Lets solve it.

Understand Circular Import

When you do simple import XYZ, it will work fine. By the time the code runs, all the modules will be imported (loaded). When you from ABC import XYZ syntax, now this module require ABC module to be fully loaded or imported before it can be imported anywhere.

In Python, import statements are executable statements.

Solving Circular Import

Lets modify our code a little bit.

# file
import jira_wrapper.jira_client as jira_client

# file
import jira_wrapper.retry_helper as retry_helper

And, if I need to access a function. I will use it like below:


If you have a class to access:


Try running now, it solves the error :)

Similar Posts

Latest Posts