473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Circular Import?

Consider the sample case:

## a.py
import d
import b
b.App()

## b.py
from c import C
B = 'B'
class App(object):pass

## c.py
from d import D
class C(object):pass

## d.py
from b import B
D = 'D'

Executing a.py will return:
Traceback (most recent call last):
File "a.py", line 1, in ?
import d
File "d.py", line 1, in ?
from b import B
File "b.py", line 1, in ?
from c import C
File "c.py", line 1, in ?
from d import D
ImportError: cannot import name D

I'm assuming this is the result of the circular imports. This isn't a
bug, right? Is there any way around it?
Jul 18 '05 #1
1 2268
Chris S. wrote:
Consider the sample case:

## a.py
import d
import b
b.App()

## b.py
from c import C
B = 'B'
class App(object):pass

## c.py
from d import D
class C(object):pass

## d.py
from b import B
D = 'D'

Executing a.py will return:
Traceback (most recent call last):
File "a.py", line 1, in ?
import d
File "d.py", line 1, in ?
from b import B
File "b.py", line 1, in ?
from c import C
File "c.py", line 1, in ?
from d import D
ImportError: cannot import name D

I'm assuming this is the result of the circular imports. This isn't a
bug, right? Is there any way around it?

Yep, circular imports only cause problems. Workarounds:
- design your application not to use circular dependencies, as these are
usually a sign for bad design
- use late imports:

## foo.py
class A:
def a():
import bar # instead of importing at the beginning of foo
bar.B()

## bar.py
import foo

def B():
pass

class C(A):
pass

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: jinal jhaveri | last post by:
Hi All, I have one question regarding circular inheritance I have 3 files 1) A.py , having module A and some other modules 2) B.py having module B and some other modules 3) C.py having...
2
by: Edward Diener | last post by:
Although it may not be good design, I have a situation such that module A imports module B and module B imports module A. This appears to be causing problems when I use Python with...
5
by: qhfgva | last post by:
I'm working with a large code base that I'm slowly trying to fix "unpythonic" features of. One feature I'm trying to fix is the use of: # how things are now...
9
by: ajikoe | last post by:
Hello, I have two modules (file1.py and file2.py) Is that ok in python (without any weird implication) if my module import each other. I mean in module file1.py there exist command import file2...
16
by: Dave S | last post by:
Anyone know how to get round the problem of circular references in VB.NET (sorry I'm a .NET newbie). I create one project which has 2 classes in it, MyHandler and MyObject. MyHandler just needs...
6
by: bvdp | last post by:
I'm going quite nutty here with an import problem. I've got a fairly complicated program (about 12,000 lines in 34 modules). I just made some "improvements" and get the following error: bob$ mma...
1
by: Carl Ganz | last post by:
I have an EXE that calls a method in a DLL. This DLL runs an import process and needs to provide feedback to a progress bar on a Form in the EXE. I can set a reference to the DLL from the EXE so...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.