473,508 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cycles between package imports

Hi

I'm a tad confused over a problem involving cycles between
packages.

Assume the following sets of files::

driver.py
a/__init__.py
a/alice.py
b/__init__.py
b/bob.py

Basically, two packages a and b. Driver simply imports one of the
two. This is the file that gets run::

,---- (driver.py)
|
| import a.alice
|
`----

The package initialization files (__init__.py) both contain
nothing. a.alice imports b.bob, and b.bob imports a.alice.

The following works fine:

,---- (a/alice.py)
|
| import b.bob
|
`----

,---- (b/bob.py)
|
| import a.alice
|
`----
However, if I change b/bob.py to import using "from" syntax, i.e.:

,---- (b/bob.py)
|
| from a import alice
|
`----

It does not work anymore::

Traceback (most recent call last):
File "driver.py", line 3, in ?
import a.alice
File "/tmp/experiments/import-cycle/a/alice.py", line 9, in ?
import b.bob
File "/tmp/experiments/import-cycle/b/bob.py", line 9, in ?
from a import alice
ImportError: cannot import name alice
So cycles between packages do not behave as cycles between
modules--there is a subtlety.

At the time that b/bob.py attempts to import a.alice, a.alice is
still being imported (i.e. executed), and there is a difference
between

import a.alice

and

from a import alice
I don't see why the reference to module a.alice could not be
available via the "from" syntax, even if it is still incompletely
initialized at the time of import.

Can anyone shed some light onto this? Is there a rule for
determining when a module becomes available to import from a
package using the "from" syntax?
Jun 17 '06 #1
4 1820
Martin Blais wrote:
Hi

I'm a tad confused over a problem involving cycles between
packages.
[lengthy example snipped]


I don't see why the reference to module a.alice could not be
available via the "from" syntax, even if it is still incompletely
initialized at the time of import.

Can anyone shed some light onto this? Is there a rule for
determining when a module becomes available to import from a
package using the "from" syntax?


It's really easy to see if you trace out, in detail, the exact
order in which things happen and when each object is initialized and
shows up in the respective module's namespace.

The general rule is: don't do that. It doesn't work, and the
hoops you have to go through to force it to work are so
complex and bizzare that they're not worth it. Redesign
the modules so you don't have cyclic dependencies.

John Roth

Jun 18 '06 #2
On 18 Jun 2006 05:25:14 -0700, John Roth <Jo*******@jhrothjr.com> wrote:
Martin Blais wrote:
Hi

I'm a tad confused over a problem involving cycles between
packages.
[lengthy example snipped]


I don't see why the reference to module a.alice could not be
available via the "from" syntax, even if it is still incompletely
initialized at the time of import.

Can anyone shed some light onto this? Is there a rule for
determining when a module becomes available to import from a
package using the "from" syntax?


It's really easy to see if you trace out, in detail, the exact
order in which things happen and when each object is initialized and
shows up in the respective module's namespace.


No it's not, at least I don't see it. The order in which the symbols
appear is different whether you're using the simple "import" form or
the "from ... import" form. This is exactly what the question is
about.
The general rule is: don't do that. It doesn't work, and the
hoops you have to go through to force it to work are so
complex and bizzare that they're not worth it. Redesign
the modules so you don't have cyclic dependencies.


This is a matter completely aside the question. Whether it's worth it
or not depends on the specific case --not included in the example--
and in the case where it shows up in my code, removing the cycle
actually made sense (it does, most of the time, but not always).

Thanks.
cheers,
Jun 21 '06 #3
In article <ma***************************************@python. org>,
"Martin Blais" <bl***@furius.ca> wrote:
On 18 Jun 2006 05:25:14 -0700, John Roth <Jo*******@jhrothjr.com> wrote:
The general rule is: don't do that. It doesn't work, and the
hoops you have to go through to force it to work are so
complex and bizzare that they're not worth it. Redesign
the modules so you don't have cyclic dependencies.
This is a matter completely aside the question.


No it isn't. I agree with John Roth.
Whether it's worth it
or not depends on the specific case --not included in the example--
and in the case where it shows up in my code, removing the cycle
actually made sense (it does, most of the time, but not always).


See, even you are unable to come up with an example where a cyclic
import makes sense.

In general, the complications they introduce are simply not worth it.
This was discovered decades ago, during the development of languages
like Ada and Modula-2 which could do automatic initialization and
finalization of library modules arranged by the compiler/linker system.
Jun 22 '06 #4
On 6/22/06, Lawrence D'Oliveiro <ld*@geek-central.gen.new_zealand> wrote:
In article <ma***************************************@python. org>,
"Martin Blais" <bl***@furius.ca> wrote:
On 18 Jun 2006 05:25:14 -0700, John Roth <Jo*******@jhrothjr.com> wrote:
The general rule is: don't do that. It doesn't work, and the
hoops you have to go through to force it to work are so
complex and bizzare that they're not worth it. Redesign
the modules so you don't have cyclic dependencies.
This is a matter completely aside the question.


No it isn't. I agree with John Roth.


See below.

Whether it's worth it
or not depends on the specific case --not included in the example--
and in the case where it shows up in my code, removing the cycle
actually made sense (it does, most of the time, but not always).


See, even you are unable to come up with an example where a cyclic
import makes sense.


This is irrelevant. I'm not interested in discussing nor debating
the merits of prohibiting or allowing cyclic imports with you nor with
anybody else, you're wasting your time.

The fact of the matter is that Python DOES allow cyclic imports, and
the way that they behave depends on the particular syntax used to
perform the import. This very specific particular item is what the
original question was about. If you want to discuss whether Python
should disallow cyclic imports to prevent users to using them, please
start your own thread.

cheers,

In general, the complications they introduce are simply not worth it.


"worth it". Subjective. Whatever.
Jul 1 '06 #5

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

Similar topics

4
4175
by: Robert Ferrell | last post by:
I have a question about how to organize modules in a package. I think there is something fundamental that I am missing. Say I'm creating a package, GreatPackage, which has three sub-packages,...
14
1556
by: ToddLMorgan | last post by:
Summary: How should multiple (related) projects be arranged (structured) and configured so that the following is possible: o Sharing common code (one of the projects would be a "common" project...
4
1387
by: David Pratt | last post by:
Hi. I have code that currently depends on a particular package of a framework. I have decided I want to create my own package because I have made many changes and it is getting too difficult to...
7
2173
by: alito | last post by:
Hi all, I am new to using packages to group my modules. I can't figure out how to run a module that uses relative imports without writing a wrapper that imports that module. Everything I try...
0
7223
marktang
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,...
0
7115
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
7321
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
7377
jinu1996
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...
1
7036
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...
1
5047
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...
0
3191
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...
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.