472,326 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,326 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 6770

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...
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__":...
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...
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...
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...
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...
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/...
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...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.