Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54203

Re: import problem

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <mohsen@pahlevanzadeh.org>
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; 'root': 0.05; 'that?': 0.05; 'attribute': 0.07; 'developer.': 0.07; 'skip:" 60': 0.07; 'skip:u 30': 0.07; '40,': 0.09; 'alias': 0.09; 'constructor': 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; 'lambda:': 0.16; 'luck!': 0.16; 'modules.': 0.16; 'nameerror:': 0.16; 'skip:i 80': 0.16; 'skip:q 30': 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; 'skip:i 40': 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; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'related': 0.29; "doesn't": 0.30; 'moved': 0.30; 'see,': 0.30; "i'm": 0.30; 'code': 0.31; 'getting': 0.31; "skip:' 10": 0.31; "d'aprano": 0.31; 'directory,': 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; 'objects': 0.35; 'but': 0.35; 'google': 0.35; 'really': 0.36; '14,': 0.36; 'are,': 0.36; 'c++': 0.36; 'skip:" 50': 0.36; 'yours,': 0.36; 'url:org': 0.36; 'should': 0.36; 'error.': 0.37; 'two': 0.37; 'being': 0.38; 'problems': 0.38; 'convention': 0.38; 'files': 0.38; 'that,': 0.38; 'url:2004': 0.39; 'recent': 0.39; 'does': 0.39; 'url:12': 0.39; 'url:3': 0.61; 'simply': 0.61; 'simple': 0.61; "you're": 0.61; 'complete': 0.62; 'name': 0.63; 'such': 0.63; '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; 'as:': 0.81; '"recursive': 0.84; 'dozens': 0.84; 'forced': 0.84; 're-design': 0.84; 'starters,': 0.84; 'url:cpython': 0.84; '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=zJcO4g8tCr+6N8NlXT1UUH2tvweX5ZRxeb4erWIZ5eE=; b=OcfSFPzzNk5rzx0uSQChsEobCF5QNNqXFvUts17fRa0TyrpHYek28/L6aSo30cbysPHH0mKx6PAVnXQbF+BdqUwNuBdt50NLq1OLIplzwtkjMpR1SZxIVr0iNsrv4Y6wCMhF8anqi4j0HL2Kwi3eGaDXpx0ACwhy9gGhVwrS2uU=;
Subject Re: import problem
From Mohsen Pahlevanzadeh <mohsen@pahlevanzadeh.org>
To Steven D'Aprano <steve+comp.lang.python@pearwood.info>
Date Mon, 16 Sep 2013 11:26:57 +0430
In-Reply-To <1379313852.24093.7.camel@debian>
References <mailman.8.1379298217.18130.python-list@python.org> <5236735f$0$29988$c3e8da3$5496439d@news.astraweb.com> <1379313852.24093.7.camel@debian>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.12.1379314628.18130.python-list@python.org> (permalink)
Lines 168
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1379314628 news.xs4all.nl 15953 [2001:888:2000:d::a6]:48124
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:54203

Show key headers only | View raw


On Mon, 2013-09-16 at 11:14 +0430, Mohsen Pahlevanzadeh wrote:
> 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 <module>
> > >     from materials.materials import *
> > >   File "/home/mohsen/codes/amlak/amlak/src/materials/materials.py", line
> > > 40, in <module>
> > >     from  ui.interface.interface import *
> > >   File "/home/mohsen/codes/amlak/amlak/src/ui/interface/interface.py",
> > > line 32, in <module>
> > >     from ui.materialsFrame import *
> > >   File "/home/mohsen/codes/amlak/amlak/src/ui/materialsFrame.py", line
> > > 24, in <module>
> > >     from ui.materialsFindFrame import *
> > >   File "/home/mohsen/codes/amlak/amlak/src/ui/materialsFindFrame.py",
> > > line 14, in <module>
> > >     from common.objects.objects import *
> > >   File "/home/mohsen/codes/amlak/amlak/src/common/objects/objects.py",
> > > line 28, in <module>
> > >     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
> Dear Steven,
> 
> I moved all of files to root of source directory, And create an put my
> constructor to objects.py file :
> #################################
> interfaceCodesObj =  interface.InterfaceCodes()
> #############################333
> And in another files that i import object such as:
> ######################
> from objects import * 
> ######################
> i get the the folloiwing traceback:
> ####################################3
> Traceback (most recent call last):
>   File "/home/mohsen/codes/amlak/amlak/src/materialsFindFrame.py", line
> 187, in <lambda>
>     QtCore.QObject.connect(self.checkBox,
> QtCore.SIGNAL(_fromUtf8("toggled(bool)")), lambda:
> interfaceCodesObj.unSetFilterDict("name"))
> NameError: global name 'interfaceCodesObj' is not defined
> Traceback (most recent call last):
>   File "/home/mohsen/codes/amlak/amlak/src/materialsFindFrame.py", line
> 192, in <lambda>
>     QtCore.QObject.connect(self.checkBox,
> QtCore.SIGNAL(_fromUtf8("toggled(bool)")), lambda:
> interfaceCodesObj.setFilterDict(self,self.checkBox,"name",self.lineEdit.text()))
> NameError: global name 'interfaceCodesObj' is not defined
> Traceback (most recent call last):
>   File "/home/mohsen/codes/amlak/amlak/src/materialsFindFrame.py", line
> 198, in <lambda>
>     QtCore.QObject.connect(self.checkBox,
> QtCore.SIGNAL(_fromUtf8("toggled(bool)")), lambda:
> interfaceCodesObj.responseToRequestForData(self))
> NameError: global name 'interfaceCodesObj' is not defined 
> #######################################################
> 
> I'm forced to used wildcard such as from~import  syntax.
> you said correctly, i was C++ developer.
> 
> Yours,
> Mohsen
> 
I solved with alias :
import foo as bar

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

import problem Mohsen Pahlevanzadeh <mohsen@pahlevanzadeh.org> - 2013-09-16 06:53 +0430
  Re: import problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-16 02:56 +0000
    Re: import problem Mohsen Pahlevanzadeh <mohsen@pahlevanzadeh.org> - 2013-09-16 08:35 +0430
    Re: import problem Mohsen Pahlevanzadeh <mohsen@pahlevanzadeh.org> - 2013-09-16 11:14 +0430
    Re: import problem Mohsen Pahlevanzadeh <mohsen@pahlevanzadeh.org> - 2013-09-16 11:26 +0430
    Re: import problem Dave Angel <davea@davea.name> - 2013-09-16 14:08 +0000

csiph-web