Consider the sample case:
## a.py
import d
import b
b.App()
## b.py
from c import C
B = 'B'
class App(object):pas s
## 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? 1 2289
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):pas s
## 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 module C and some other modules
|
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 mod_python/Apache and PSP programming. Can
anybody tell me if there are any situations where circular imports cause
problems and, other than a redesign to eliminate it, if there are any other
ways around those problems ?
|
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
sys.path.append('/general/path/aaa/bbb') # lots of lines like this to
specific dirs
import foo
|
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 and in module file2.py there exist command import file1?
This is not working in C#.
pujo
|
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 to call another class in a different project and pass a
parameter of type MyObject to it.
So the class in the different project needs to import the first project
namespace so it know what MyObject is.
The MyHandler class needs to import the second...
| |
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
Traceback (most recent call last):
File "/usr/local/bin/mma", line 55, in <module>
import MMA.main
File "/usr/local/share/mma/MMA/main.py", line 33, in <module>
import MMA.docs
|
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 the EXE can
call the import method but a circular reference is caused when I
attempt to set a project reference to the EXE project from the DLL
project. I now have a DLL which cannot get a reference to the Form in
the EXE to update the progress...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |