From xiaogang at gridmo.com Thu Jul 14 11:08:32 2005 From: xiaogang at gridmo.com (John Xiaogang Zhang) Date: Thu, 14 Jul 2005 19:08:32 +1000 Subject: [summerofcode] Problem in import Message-ID: <42D62B90.4090300@gridmo.com> Hi all, I am a newbe in Python, and sometimes have problem with import. Now I have a line from tilefilesys.tilefile import TFHandlers in the top level module test.py. I have also tried to use import tilefilesys.tilefile.TFHandlers as TFHandlers but the behaviour seems exactly the same. The code in test.py calls other modules, some of them also use the similar import statement, and I got the following error message: Traceback (most recent call last): File "", line 1, in ? File "test.py", line 29, in test from tilefilesys.tilefile import TFHandlers File "/home/xzhang/dev/TS/tilefilesys/tilefile/TFHandlers.py", line 28, in ? from tilefilesys.tilefile.FmtVersn import * File "/home/xzhang/dev/TS/tilefilesys/tilefile/FmtVersn.py", line 163, in ? defaultVersnModule = loadTFModule( defaultVersionStr ) File "/home/xzhang/dev/TS/tilefilesys/tilefile/FmtVersn.py", line 156, in loadTFModule return dynImport(__baseTFPackage + __availableVersions[versnStr]) File "/home/xzhang/dev/TS/tilefilesys/common.py", line 401, in dynImport mod = __import__(modName, globals(), locals(), []) File "/home/xzhang/dev/TS/tilefilesys/tilefile/v1/__init__.py", line 26, in ? import TFs File "/home/xzhang/dev/TS/tilefilesys/tilefile/v1/TFs.py", line 27, in ? from Fields import * File "/home/xzhang/dev/TS/tilefilesys/tilefile/v1/Fields.py", line 33, in ? from tilefilesys.tilefile import TFHandlers ImportError: cannot import name TFHandlers If I change the line in the last file (Fields.py) to import tilefilesys.tilefile.TFHandlers as TFHandlers then the error becomes File "/home/xzhang/dev/TS/tilefilesys/tilefile/v1/Fields.py", line 32, in ? import tilefilesys.tilefile.TFHandlers as TFHandlers AttributeError: 'module' object has no attribute 'TFHandlers' However, if I change the last file to import tilefilesys.tilefile.TFHandlers as TFHandlers that is, without the "as TFHandlers", then the errors gone (but I cannot refer it as the shorter reference). Could please anyone tell me why it happens, and how to get rid off it? Thank you in advance. Xiaogang From ianb at colorstudy.com Thu Jul 14 17:25:56 2005 From: ianb at colorstudy.com (Ian Bicking) Date: Thu, 14 Jul 2005 10:25:56 -0500 Subject: [summerofcode] Problem in import In-Reply-To: <42D62B90.4090300@gridmo.com> References: <42D62B90.4090300@gridmo.com> Message-ID: <42D68404.8090609@colorstudy.com> John Xiaogang Zhang wrote: > import tilefilesys.tilefile.TFHandlers as TFHandlers > AttributeError: 'module' object has no attribute 'TFHandlers' > > However, if I change the last file to > > import tilefilesys.tilefile.TFHandlers as TFHandlers > > that is, without the "as TFHandlers", then the errors gone (but I cannot > refer it as the shorter reference). > > Could please anyone tell me why it happens, and how to get rid off it? Looks like a circular import; probably you import tilefile from somewhere, and tilefile imports this file, and this file then tries to import tilefile. During that third import (the one with the error) the tilefile module has not been fully loaded, so the TFHandlers object has not yet been created. You'll have to refactor your modules to avoid circular imports. -- Ian Bicking / ianb at colorstudy.com / http://blog.ianbicking.org