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


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

Re: Question about circular imports

Started by"Frank Millman" <frank@chagford.com>
First post2012-02-27 08:16 +0200
Last post2012-02-28 09:58 -0800
Articles 3 — 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: Question about circular imports "Frank Millman" <frank@chagford.com> - 2012-02-27 08:16 +0200
    Re: Question about circular imports "OKB (not okblacke)" <brenNOSPAMbarn@NObrenSPAMbarn.net> - 2012-02-28 03:55 +0000
      Re: Question about circular imports Ethan Furman <ethan@stoneleaf.us> - 2012-02-28 09:58 -0800

#20923 — Re: Question about circular imports

From"Frank Millman" <frank@chagford.com>
Date2012-02-27 08:16 +0200
SubjectRe: Question about circular imports
Message-ID<mailman.189.1330323422.3037.python-list@python.org>
>
> To avoid the tedious reference, follow this with
> read = sound.formats.wavread # choose the identifier you prefer
>

@Terry and OKB

I tried that, but it does not work.

a.py
/b
    __init__.py
    c.py
   d.py

a.py -
    from b import c
c.py -
    import b.d
d.py -
    import b.c

If I run a.py, it returns with no error.

c.py -
    import b.d
    d = b.d
d.py -
    import b.c
    c = b.c

If I run a.py, I get

Traceback (most recent call last):
  File "F:\tests\a.py", line 1, in <module>
    from b import c
  File "F:\tests\b\c.py", line 1, in <module>
    import b.d
  File "F:\tests\b\d.py", line 2, in <module>
    c = b.c
AttributeError: 'module' object has no attribute 'c'

I get the same if I try 'import b.c as c'.

Frank


[toc] | [next] | [standalone]


#20966

From"OKB (not okblacke)" <brenNOSPAMbarn@NObrenSPAMbarn.net>
Date2012-02-28 03:55 +0000
Message-ID<XnsA006CA54C1864OKB@88.198.244.100>
In reply to#20923
Frank Millman wrote:

>>
>> To avoid the tedious reference, follow this with
>> read = sound.formats.wavread # choose the identifier you prefer
>>
> 
> @Terry and OKB
> 
> I tried that, but it does not work.
> 
> a.py
> /b
>     __init__.py
>     c.py
>    d.py
> 
> a.py -
>     from b import c
> c.py -
>     import b.d
> d.py -
>     import b.c
> 
> If I run a.py, it returns with no error.
> 
> c.py -
>     import b.d
>     d = b.d
> d.py -
>     import b.c
>     c = b.c
> 
> If I run a.py, I get
> 
> Traceback (most recent call last):
>   File "F:\tests\a.py", line 1, in <module>
>     from b import c
>   File "F:\tests\b\c.py", line 1, in <module>
>     import b.d
>   File "F:\tests\b\d.py", line 2, in <module>
>     c = b.c
> AttributeError: 'module' object has no attribute 'c'
> 
> I get the same if I try 'import b.c as c'.

    	Interesting, you're right.  Note that it will work in c.py but not 
in d.py

    	Anyway, testing this just reinforced my distaste for circular 
imports.  Just trying to think about how it ought to work with a 
importing c but then c and d importing each other makes my brain hurt.  
Refactoring the files so that common code is in a separate library 
imported by both is easier to understand, and has the nice side bonus 
that it works.

-- 
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead.  Go, instead, where there is
no path, and leave a trail."
	--author unknown

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


#20998

FromEthan Furman <ethan@stoneleaf.us>
Date2012-02-28 09:58 -0800
Message-ID<mailman.254.1330455105.3037.python-list@python.org>
In reply to#20966
OKB (not okblacke) wrote:
>     	Anyway, testing this just reinforced my distaste for circular 
> imports.  Just trying to think about how it ought to work with a 
> importing c but then c and d importing each other makes my brain hurt.  
> Refactoring the files so that common code is in a separate library 
> imported by both is easier to understand, and has the nice side bonus 
> that it works.
> 

+1 QOTW

[toc] | [prev] | [standalone]


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


csiph-web