473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Recursive import within packages


all examples performed with:

Python 2.3+ (#2, Aug 10 2003, 11:09:33)
[GCC 3.3.1 20030728 (Debian prerelease)] on linux2
(2, 3, 0, 'final', 1)

This is a recursive import:

-- blapp.py --
print "Blapp start"
import blupp
print "Blapp end"

-- blupp.py --
print "Blupp start"
import blapp
print "Blupp end"
This works like a charm:
import blapp Blapp start
Blupp start
Blupp end
Blapp end
The same files with a directory thing with __init__:
from thing import blapp Blapp start
Blupp start
Blupp end
Blapp end

Changing the imports to import thing.blupp and import thing.blapp:
from thing import blapp Blapp start
Blupp start
Blupp end
Blapp end

Adding a print of the module with:

print "Blapp end", blupp
results in:
import blapp Blapp start
Blupp start
Blupp end <module 'blapp' from 'blapp.py'>
Blapp end <module 'blupp' from 'blupp.py'>


Changing to:

import thing.blapp as blapp

and vice versa

from thing import blapp Blapp start
Blupp start
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "thing/blapp.py", line 2, in ?
import thing.blupp as blupp
File "thing/blupp.py", line 2, in ?
import thing.blapp as blapp
AttributeError: 'module' object has no attribute 'blapp'

Changing to:

from thing import blapp

and vice versa

from thing import blapp Blapp start
Blupp start
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "thing/blapp.py", line 2, in ?
from thing.blupp import blupp
File "thing/blupp.py", line 2, in ?
from thing import blapp
ImportError: cannot import name blapp


And finally:

print "Blapp start"
import thing.blupp
print "Blapp end", thing.blupp
(and vice versa)

from thing import blapp Blapp start
Blupp start
Blupp end
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "thing/blapp.py", line 2, in ?
import thing.blupp
File "thing/blupp.py", line 3, in ?
print "Blupp end", thing.blapp
AttributeError: 'module' object has no attribute 'blapp'
Adding a print of dir(thing) verifies this:

-- blapp.py -- (and vice versa)
print "Blapp start"
import thing.blupp
print dir(thing)
print "Blapp end"

import thing.blupp Blupp start
Blapp start
['__builtins__', '__doc__', '__file__', '__name__', '__path__']
Blapp end
['__builtins__', '__doc__', '__file__', '__name__', '__path__', 'blapp']
Blupp end

-- blapp.py --

print "Blapp start"
import thing.blupp
import thing.notexisting
print "Blapp end", thing.blupp
from thing import blupp

Blupp start
Blapp start
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "thing/blupp.py", line 2, in ?
import thing.blapp
File "thing/blapp.py", line 3, in ?
import thing.notexisting
ImportError: No module named notexisting

Now, could anyone please explain to me the logic involved here?

Why is circular import only valid when accessing them without package
name? It clearly works by not using full references, even to print the
module while the other module is being loaded.

The user-fix to the problem is of course:

a) Don't use circular references
b) delay circular import until module usage
(ie. within class/function)
c) Use relative modulenames (works only within the same package)
d) import thing.blapp - but don't expect thing.blapp to be there
until module usage (ie. within class/function)
But my question is why this works outside packages, but not inside
packages. This must be a bug, but I can't find it anywhere in the bugs
section on SourceForge.

(A google search for 'circular import' on Google seems impossible =) Way
too many circle-square-shape-examples!)

The FAQ says:

Circular imports are fine where both modules use the "import <module>"
form of import. They fail when the 2nd module wants to grab a name
out of the first ("from module import name") and the import is at
the top level. That's because names in the 1st are not yet available,
(the first module is busy importing the 2nd).

However that is not correct in this case, the "thing" module is loaded
and works fine, it is the name thing.blapp that is not registered until
after blapp is loaded. In my opinion thing.blapp should be bound before
the actuall import of thing.blapp.

Actually, thing.blapp gets registered into sys.modules right away.
--
Stian Sřiland Being able to break security doesn't make
Trondheim, Norway you a hacker more than being able to hotwire
http://stain.portveien.to/ cars makes you an automotive engineer. [ESR]
Jul 18 '05 #1
0 6879

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

Similar topics

16
by: Manlio Perillo | last post by:
Hi. I'm a new user of Python but I have noted a little problem. Python is a very good language but it is evolving, in particular its library is evolving. This can be a problem when, ad example,...
5
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for...
1
by: mirandacascade | last post by:
O/S: Windows 2K Vsn of Python: 2.4 Currently: 1) Folder structure: \workarea\ <- ElementTree files reside here \xml\ \dom\
0
by: Anders J. Munch | last post by:
Now 2.5 is out, and we have a syntax for explicit relative imports (PEP 328, http://www.python.org/dev/peps/pep-0328/, in case anyone wasn't paying attention). The long-term plan is that the...
5
by: Jandre | last post by:
Hi I am a python novice and I am trying to write a python script (most of the code is borrowed) to Zip a directory containing some other directories and files. The script zips all the files fine...
12
by: =?Utf-8?B?am9uaWdy?= | last post by:
I wrote a simple VB.NET application that imports and edits CSV files. Now I’d like to “lock” the raw (pre-import) CSV files so these cannot be opened separately. It is not high-sensitive...
2
by: sebastien.abeille | last post by:
Hello, I would like to create a minimalist file browser using pyGTK. Having read lot of tutorials, it seems to me that that in my case, the best solution is to have a gtk.TreeStore containing...
5
by: luca72 | last post by:
Hello i can't import cherrypy2 but i don't know why this is the sys path: '', '/home/pirataja/opo.net/python/lib/python2.5/site-packages/ setuptools-0.6c7-py2.5.egg',...
3
by: jrh | last post by:
Hello, From previous posts and documentation it seems python should be able to import a module that is compiled into a .dll just as well as a .pyd. I have a pyd that works fine, but after...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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...
0
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,...
0
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...

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.