Path: csiph.com!usenet.pasdenom.info!goblin1!goblin.stu.neva.ru!news.astraweb.com!border2.a.newsrouter.astraweb.com!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; 'interpreter': 0.05; 'that?': 0.05; 'attribute': 0.07; 'skip:" 60': 0.07; 'skip:u 30': 0.07; '40,': 0.09; 'mess': 0.09; 'obj': 0.09; 'python:': 0.09; 'skip:# 30': 0.09; 'developer': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'assume': 0.14; '24,': 0.16; '32,': 0.16; 'effect,': 0.16; 'exactly,': 0.16; 'fine.': 0.16; 'imported.': 0.16; 'luck!': 0.16; 'modules.': 0.16; 'sorts': 0.16; 'subject:import': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'url:decimal': 0.16; 'url:file': 0.16; 'url:py': 0.16; 'written.': 0.16; 'files.': 0.16; 'skip:# 20': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'code.': 0.18; 'obviously': 0.18; 'all,': 0.19; 'module': 0.19; 'trying': 0.19; 'file,': 0.19; "hasn't": 0.19; 'packages.': 0.19; 'small,': 0.19; 'split': 0.19; 'code,': 0.22; 'import': 0.22; 'comfortable': 0.22; 'putting': 0.22; 'python?': 0.22; 'cc:addr:python.org': 0.22; '31,': 0.24; 'subject:problem': 0.24; 'java': 0.24; 'math': 0.24; 'mon,': 0.24; 'file.': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'source': 0.25; 'this:': 0.26; 'second': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'related': 0.29; "doesn't": 0.30; 'see,': 0.30; 'code': 0.31; 'getting': 0.31; "d'aprano": 0.31; 'fighting': 0.31; 'sep': 0.31; 'steven': 0.31; 'file': 0.32; 'class': 0.32; 'another': 0.32; 'url:python': 0.33; '(most': 0.33; 'becomes': 0.33; 'guess': 0.33; 'not.': 0.33; 'third': 0.33; 'problem': 0.35; 'classes': 0.35; 'skip:u 20': 0.35; 'something': 0.35; 'but': 0.35; 'google': 0.35; 'really': 0.36; '14,': 0.36; 'are,': 0.36; 'c++': 0.36; 'skip:" 50': 0.36; 'url:org': 0.36; 'should': 0.36; 'error.': 0.37; 'two': 0.37; 'being': 0.38; 'thank': 0.38; 'problems': 0.38; 'convention': 0.38; 'that,': 0.38; 'url:2004': 0.39; 'recent': 0.39; 'does': 0.39; 'url:12': 0.39; 'how': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'problems.': 0.60; 'url:3': 0.61; 'simply': 0.61; 'simple': 0.61; "you're": 0.61; 'complete': 0.62; 'card': 0.63; 'maximum': 0.63; 'skip:n 10': 0.64; 'different': 0.65; 'dear': 0.65; 'between': 0.67; 'close': 0.67; 'day': 0.76; '"recursive': 0.84; 'dozens': 0.84; 're-design': 0.84; 'starters,': 0.84; 'url:cpython': 0.84; '"how': 0.91; 'many,': 0.93; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=pahlevanzadeh.org; s=default; h=Content-Transfer-Encoding:Mime-Version:Content-Type:References:In-Reply-To:Date:Cc:To:From:Subject:Message-ID; bh=s3q3Igp46TF5JkzHVkLu8bwGNEpvTIFeAFPFXEyF9H0=; b=kR+zpuuGXNmJWjbTo95J0wcnf/5vIqHrCjfHk/WfMUYxJgFtJwIPrsas2BbuJyIQxNi9al69wMPVqLaXybfpg4VzSaTk2AzkKxmgNSjkvopgR+ZZQ8NvPT0Dk7TJD/kCmcGB/8m0QRXgfLzvxlvi1QEKeb3cqo7uwCJk29LLef0=; Subject: Re: import problem From: Mohsen Pahlevanzadeh To: Steven D'Aprano Date: Mon, 16 Sep 2013 08:35:04 +0430 In-Reply-To: <5236735f$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <5236735f$0$29988$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.4.4-4+b1 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - part7.royal-servers.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - pahlevanzadeh.org X-Get-Message-Sender-Via: part7.royal-servers.com: authenticated_id: mohsen@pahlevanzadeh.org Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 126 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1379304321 news.xs4all.nl 15936 [2001:888:2000:d::a6]:54539 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54199 thank you, you gave me "how to get fish" instead of "fish", it's very better. On Mon, 2013-09-16 at 02:56 +0000, Steven D'Aprano wrote: > On Mon, 16 Sep 2013 06:53:26 +0430, Mohsen Pahlevanzadeh wrote: > > > Dear all, > > > > i have the following two line codes: > > ############################ > > import ui.interface.interface > > obj = ui.interface.interface.InterfaceCodes() > > ###########################333 > > I have same code in another package and work fine. but i get the : > > > > #####################################################3 > > This traceback following suggests that your package is a complete tangled > mess of wild card imports. Perhaps I am misreading something, but the > following suggests that your package is highly coupled, with strong > dependencies between different modules. This is a poor design and will > give you many, many problems. As you are already having. > > Do you understand what I mean when I talk about modules being "highly > coupled"? > > Are you a Java or C++ developer learning Python? Your code suggests to me > that you might be. If you are, you should read these to get some ideas of > how your Java intuitions will lead you astray in Python: > > http://dirtsimple.org/2004/12/python-is-not-java.html > http://dirtsimple.org/2004/12/java-is-not-python-either.html > > > I assume that everything under the "amlak" directory is your code. Is > that correct? > > Here's the traceback again: > > > Traceback (most > > recent call last): > > File "./main.py", line 31, in > > from materials.materials import * > > File "/home/mohsen/codes/amlak/amlak/src/materials/materials.py", line > > 40, in > > from ui.interface.interface import * > > File "/home/mohsen/codes/amlak/amlak/src/ui/interface/interface.py", > > line 32, in > > from ui.materialsFrame import * > > File "/home/mohsen/codes/amlak/amlak/src/ui/materialsFrame.py", line > > 24, in > > from ui.materialsFindFrame import * > > File "/home/mohsen/codes/amlak/amlak/src/ui/materialsFindFrame.py", > > line 14, in > > from common.objects.objects import * > > File "/home/mohsen/codes/amlak/amlak/src/common/objects/objects.py", > > line 28, in > > obj = ui.interface.interface.InterfaceCodes() > > AttributeError: 'module' object has no attribute 'interface' > > ########################################### > > > So your main module does a wild-card import from materials.materials, > which does a wild-card import from ui.interface.interface, which does a > wild-card import from ui.materialsFrame, which does a wild-card import > from ui.materialsFindFrame, which does a wild-card import from > common.objects.objects, which requires ui.interface.interface to have > already been loaded and imported. But it hasn't been, because it is still > being imported. > > The *immediate* problem is that, in effect, before you can import > ui.interface.interface, you need to import ui.interface.interface. > Obviously this is going to cause you problems. Google on "recursive > imports" to learn about the sorts of problems and how to avoid them. > > The second problem is that, in general, you should try to avoid wild-card > imports. They're not always bad, but they were really invented for use in > the interactive interpreter so you can do things like this: > > from math import * > sqrt(42) > sin(1.5) > > > Using them inside non-interactive code is not forbidden exactly, but it > is frowned upon since it makes understanding your code harder. > > The third problem is that your code layout looks like you are fighting > Python, trying to force it to be something it is not. For starters, if > you're writing packages that look like this: > > ui.interface.interface > > that's simply poor design for Python. My guess is that you might be > following the Java convention of putting exactly one class per source > file. That is not the way Python code should be written. Modules should > contain all the related classes and functions, at least up to the point > that the module becomes so large that it is painful to work with. How > large is that? In my opinion, this is getting close to the maximum I > personally would be comfortable with: > > http://hg.python.org/cpython/file/3.3/Lib/decimal.py > > > although some people might be happy with large files. > > But what is important is that related code should be together in the one > file, not split across multiple modules and multiple packages. > > If you are trying to "future proof" your code, and thinking "today it is > small, but one day it will be big and will need to be a package with > dozens of modules", that doesn't matter. Don't write the code you think > you will need in five years. Write the code you need now. Google "YAGNI" > for more. > > I am sorry that I cannot just give you a simple one-line fix for your > error. As far as I can see, the fix is to re-design the package so that > it is flatter, with fewer imports, and avoid the recursive import. > > Good luck! > > > > -- > Steven