python|March 08, 2022|1 min read

How to Solve Circular Import Error in Python

TL;DR

Fix Python circular import errors by moving the import statement inside the function that needs it (lazy import) instead of placing it at the top of the module.

How to Solve Circular Import Error in Python

Introduction

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

  • jira_client.py (have a class JiraClient)
  • retry_helper.py

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

# file retry_helper.py
from jira_wrapper.jira_client import JiraClient

# file jira_client.py
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/jira_client.py)

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 retry_helper.py
import jira_wrapper.jira_client as jira_client

# file jira_client.py
import jira_wrapper.retry_helper as retry_helper

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

retry_helper.retry_if_xyz

If you have a class to access:

jira_client.JiraClient

Try running now, it solves the error :)

Related Posts

Python 3 - Magical Methods and Tricks with extremely useful methods

Python 3 - Magical Methods and Tricks with extremely useful methods

This post will show some really awesome tricks in python. Get the power of a…

Python 3 - Fun with Python String

Python 3 - Fun with Python String

This post some useful tips of using strings, and some issues while dealing with…

Python 3 - Format String fun

Python 3 - Format String fun

This post is dedicated for cases where we intend to append a variable value in a…

Understanding and Solving pylint errors for Python 3

Understanding and Solving pylint errors for Python 3

Pylint is an excellent tool to have good quality code. This post is not about…

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Introduction This post has the complete code to send email through smtp server…

Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Introduction It is very important to introduce few process so that your code and…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…