473,799 Members | 2,834 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The imp module and cyclic imports

Hi All,

I'm having a little bit of trouble using the "imp" module to
dynamically import modules. It seems that somehow cyclic references of
modules don't work.
I'm unable to get the following to work:

I've got the following files:

web/__init__.py
web/one.py
web/two.py
testimport.py
From which web/one.py contains the line:


from web import two

and web/two.py contains the line:

from web import one

..

Now I try to import one of the two modules in testimport.py:

import imp
(mfile,pathname ,description) = imp.find_module ("one", ["web"])
m = imp.load_module ("one",mfile,pa thname,descript ion)

When I start the file testimport.py, I get the following exception:

$ python testimport.py
Traceback (most recent call last):
File "testimport.py" , line 3, in ?
m = imp.load_module ("one",mfile,pa thname,descript ion)
File "web/one.py", line 1, in ?
from web import two
File "/scripts/python/importtest/web/two.py", line 2, in ?
from web import one
File "web/one.py", line 1, in ?
from web import two
ImportError: cannot import name two

It seems that when two.py (referenced by one.py) references one.py
again, something breaks.

I'm lost. Am I doing something wrong, or is this a bug?

Many thanks for any light anybody can shed on this.

Greetings

Matthias

Nov 24 '05 #1
2 2155
Matthias Kramm wrote:
I'm having a little bit of trouble using the "imp" module to
dynamically import modules. It seems that somehow cyclic
references of modules don't work.
the problem you're seeing appears also if you use "import web.one"
or "from web import one" or "__import__('we b.one')".
I'm unable to get the following to work:

I've got the following files:

web/__init__.py
web/one.py
web/two.py
testimport.py
From which web/one.py contains the line:


from web import two

and web/two.py contains the line:

from web import one


I think, but I'm not 100% sure, that the problem you're seeing is
that Python hasn't finished importing the "web.one" module when
you're trying to import it again.

- testimport wants to import web.one
- python imports the web module
- python finishes importing the web module
- python imports the web.one module
- web.one wants to import web.two
- python imports the web.two module
- web.two wants to import web.one
- python notices that web.one is already being imported, and
leaves it to the original import to finish the task

if you replace the "from web import" statements with plain imports,
everything will work as expected. just change

from web import one

to

import one

and do the same for the other module.

hope this helps!

</F>

Nov 25 '05 #2
> the problem you're seeing appears also if you use "import web.one"
or "from web import one" or "__import__('we b.one')".
Thanks for the hint. You're right. This isn't actually imp related. The
standard import also fails.
if you replace the "from web import" statements with plain imports,
everything will work as expected. just change

from web import one

to

import one


Unfortunately, this fails if one.py and two.py are in different
directories/packages.
With a setup like
web1/one.py
web2/two.py
there doesn't seem to be any way to make one.py and two.py reference
each other via (non-delayed) imports.

It's interesting, though, that cyclic imports work when using the plain
"import foo" import, but not with the "from package import foo" style.
Especially since the former also used to fail (google for "python
cyclic imports" on groups.google.c om). I wonder whether the "from"
style imports were overlooked when the cyclic problem was fixed.

Greetings

Matthias

Nov 27 '05 #3

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

Similar topics

10
1916
by: Ron | last post by:
Hello, The following code works in a Form class module but not a standard module: Dim d1 As DateTime Format(d1, "MMMM") In a standard module I get a squigly line saying that an argument has not been specified for parameter format... So I tried this:
2
2760
by: jimmyfishbean | last post by:
Hi, I have created a VB.Net windows service. Everything was working fine until I attempted to add a setup project. Now, for some strange, when I try and run the application (using INSTALLUTIL to install/uninstall it) I get the error: (53) The specified module could not be found When I debug the code, this error is thrown when the following code
3
1409
by: John Henry | last post by:
Hi list, I am trying to understand better Python packaging. This might be a messed up class hierachy but how would I break this cyclic relatioship? In file A: from B import B_Class Class_A_Main():
13
2298
by: Rafe | last post by:
Hi, I am in a situation where I feel I am being forced to abandon a clean module structure in favor of a large single module. If anyone can save my sanity here I would be forever grateful. My problem is that classes in several modules share a common base class which needs to implement a factory method to return instances of these same classes.
0
10485
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, 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...
0
10252
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 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...
1
10231
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9073
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, 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...
1
7565
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 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...
0
6805
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();...
0
5463
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...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.