Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #49852 > unrolled thread

Re: Coping with cyclic imports

Started bykanchan.n.mahajan@gmail.com
First post2013-07-04 05:48 -0700
Last post2013-07-04 18:12 -0400
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Coping with cyclic imports kanchan.n.mahajan@gmail.com - 2013-07-04 05:48 -0700
    Re: Coping with cyclic imports Dave Angel <davea@davea.name> - 2013-07-04 09:33 -0400
    Re: Coping with cyclic imports Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-07-04 16:03 +0100
      Re: Coping with cyclic imports kanchan.n.mahajan@gmail.com - 2013-07-04 08:11 -0700
        Re: Coping with cyclic imports Dave Angel <davea@davea.name> - 2013-07-04 18:12 -0400

#49852 — Re: Coping with cyclic imports

Fromkanchan.n.mahajan@gmail.com
Date2013-07-04 05:48 -0700
SubjectRe: Coping with cyclic imports
Message-ID<bc7429c5-7771-4529-9554-f494f393cbb6@googlegroups.com>
On Tuesday, April 8, 2008 10:06:46 PM UTC+2, Torsten Bronger wrote:
> Hallöchen!
> 
> I have a rather fat module that represents a document parser --
> inline elements, block elements, and the like.  Now I want to split
> it into many modules to make everything more manageable.
> 
> But at the moment I don't see how to avoid cyclic imports:  A
> document element A, which is represented my module parser.A, may
> contain the element B, as defined in parser.B.  And vice versa.  So
> both modules must import each other, as far as I can see.
> 
> I know that cyclic imports work in Python under certain
> circumstances.  Can anyone refer me to a page which explains *when*
> this works?  Because at least once, the imported module was not
> "finished" and thus largely unusual.
> 
> Thank you!
> 
> Tschö,
> Torsten.
> 


If you do "import foo" inside bar and "import bar" inside foo, it will work fine. By the time anything actually runs, both modules will be fully loaded and will have references to each other.

The problem is when instead you do "from foo import abc" and "from bar import xyz". Because now each module requires the other module to already be compiled (so that the name we are importing exists) before it can be compiled.

from
http://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python

[toc] | [next] | [standalone]


#49860

FromDave Angel <davea@davea.name>
Date2013-07-04 09:33 -0400
Message-ID<mailman.4228.1372944826.3114.python-list@python.org>
In reply to#49852
On 07/04/2013 08:48 AM, kanchan.n.mahajan@gmail.com wrote:
> On Tuesday, April 8, 2008 10:06:46 PM UTC+2, Torsten Bronger wrote:
>> Hallöchen!
>>
>> I have a rather fat module that represents a document parser --
>> inline elements, block elements, and the like.  Now I want to split
>> it into many modules to make everything more manageable.
>>
>> But at the moment I don't see how to avoid cyclic imports:  A
>> document element A, which is represented my module parser.A, may
>> contain the element B, as defined in parser.B.  And vice versa.  So
>> both modules must import each other, as far as I can see.
>>
>> I know that cyclic imports work in Python under certain
>> circumstances.  Can anyone refer me to a page which explains *when*
>> this works?  Because at least once, the imported module was not
>> "finished" and thus largely unusual.
>>
>> Thank you!
>>
>> Tschö,
>> Torsten.
>>
>
>
> If you do "import foo" inside bar and "import bar" inside foo, it will work fine. By the time anything actually runs, both modules will be fully loaded and will have references to each other.
>
> The problem is when instead you do "from foo import abc" and "from bar import xyz". Because now each module requires the other module to already be compiled (so that the name we are importing exists) before it can be compiled.
>
> from
> http://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python
>

That's a nice looking oversimplification.  But it's not true if 
top-level code in the second module to be imported tries to use symbols 
defined in the first module.  And it's definitely bad practice, with 
weird bugs if either one of these files is the main script being loaded.


-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#49871

FromOscar Benjamin <oscar.j.benjamin@gmail.com>
Date2013-07-04 16:03 +0100
Message-ID<mailman.4236.1372950228.3114.python-list@python.org>
In reply to#49852
On 4 July 2013 13:48,  <kanchan.n.mahajan@gmail.com> wrote:
> On Tuesday, April 8, 2008 10:06:46 PM UTC+2, Torsten Bronger wrote:
[snip]
>
> If you do "import foo" inside bar and "import bar" inside foo, it will work fine. By the time anything actually runs, both modules will be fully loaded and will have references to each other.
>
> The problem is when instead you do "from foo import abc" and "from bar import xyz". Because now each module requires the other module to already be compiled (so that the name we are importing exists) before it can be compiled.
>
> from
> http://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python

Is there some reason you're responding to a post from 5 years ago?

Or is it just a joke that you've created a cyclic import advice link
by referring to a SO question where the top answer is actually a quote
linking back to the previous post in this same thread?


Oscar

[toc] | [prev] | [next] | [standalone]


#49873

Fromkanchan.n.mahajan@gmail.com
Date2013-07-04 08:11 -0700
Message-ID<c3f6fd9a-9410-4f59-827d-3c76f25644e5@googlegroups.com>
In reply to#49871
On Thursday, July 4, 2013 5:03:20 PM UTC+2, Oscar Benjamin wrote:
> On 4 July 2013 13:48,  <kanchan.n.mahajan@gmail.com> wrote:
> 
> > On Tuesday, April 8, 2008 10:06:46 PM UTC+2, Torsten Bronger wrote:
> 
> [snip]
> 
> >
> 
> > If you do "import foo" inside bar and "import bar" inside foo, it will work fine. By the time anything actually runs, both modules will be fully loaded and will have references to each other.
> 
> >
> 
> > The problem is when instead you do "from foo import abc" and "from bar import xyz". Because now each module requires the other module to already be compiled (so that the name we are importing exists) before it can be compiled.
> 
> >
> 
> > from
> 
> > http://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python
> 
> 
> 
> Is there some reason you're responding to a post from 5 years ago?
> 
> 
> 
> Or is it just a joke that you've created a cyclic import advice link
> 
> by referring to a SO question where the top answer is actually a quote
> 
> linking back to the previous post in this same thread?
> 
> 
> 
> 
> 
> Oscar

Well I am just a new to python
and sorry i didnt see the date of the question
and guessing if people find this post first then it would be helpful for them to get a good answer

[toc] | [prev] | [next] | [standalone]


#49906

FromDave Angel <davea@davea.name>
Date2013-07-04 18:12 -0400
Message-ID<mailman.4253.1372975953.3114.python-list@python.org>
In reply to#49873
On 07/04/2013 11:11 AM, kanchan.n.mahajan@gmail.com wrote:
> On Thursday, July 4, 2013 5:03:20 PM UTC+2, Oscar Benjamin wrote:
>> On 4 July 2013 13:48,  <kanchan.n.mahajan@gmail.com> wrote:
>>
>>> On Tuesday, April 8, 2008 10:06:46 PM UTC+2, Torsten Bronger wrote:
>>
>> [snip]
>>
>>>
>>
>>> If you do "import foo" inside bar and "import bar" inside foo, it will work fine. By the time anything actually runs, both modules will be fully loaded and will have references to each other.
>>
>>>
>>
>>> The problem is when instead you do "from foo import abc" and "from bar import xyz". Because now each module requires the other module to already be compiled (so that the name we are importing exists) before it can be compiled.
>>
>>>
>>
>>> from
>>
>>> http://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python
>>
>>
>>
>> Is there some reason you're responding to a post from 5 years ago?
>>
>>
>>
>> Or is it just a joke that you've created a cyclic import advice link
>>
>> by referring to a SO question where the top answer is actually a quote
>>
>> linking back to the previous post in this same thread?
>>
>>
>>
>>
>>
>> Oscar
>
> Well I am just a new to python
> and sorry i didnt see the date of the question
> and guessing if people find this post first then it would be helpful for them to get a good answer
>

But the StackOverflow answers for this question aren't good enough.

The problem is much more subtle than those answers imply, unless you 
make certain assumptons about the user's code.  And if I'm going to do 
that, the thing I'm going to figure is that the rational user avoids any 
cyclic imports, period.

if a.py imports b.py, then b.py is not permitted, directly or 
indirectly, to import a.py.  If it does, and something doesn't work, 
then 50 lashes with a wet noodle.

Whenever you find a cycle, try to move the common stuff into a 3rd 
module and have the first two import that one instead of each other.

Avoid having any non-trivial code at the top level, and in no case 
reference anything you've imported from there.  Move everything into 
functions, and don't have any class-static code or data initialization. 
  If you have to break any of these (such as referring to sys.argv), 
make sure that the module you're using is not one that's importing you.

Never, ever import the original script from another module.


-- 
DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web