Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!feeder.erje.net!1.eu.feeder.erje.net!ecngs!feeder2.ecngs.de!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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; 'syntax': 0.04; 'beginner': 0.05; 'explicit': 0.07; 'method.': 0.07; 'python3': 0.07; 'referring': 0.07; '%s"': 0.09; 'debugging.': 0.09; 'imported': 0.09; 'okay': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '"from': 0.16; '(it': 0.16; 'compatible,': 0.16; 'dependency,': 0.16; 'foo"': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'grep': 0.16; 'imports': 0.16; 'logger': 0.16; 'package).': 0.16; 'partly': 0.16; 'subject:fails': 0.16; 'subject:import': 0.16; 'wrote:': 0.18; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'circular': 0.24; 'helper': 0.24; 'library,': 0.24; 'module,': 0.24; 'package.': 0.24; 'fairly': 0.24; 'question': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'accidentally': 0.31; 'another': 0.32; 'problem': 0.35; 'subject:with': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'doing': 0.36; 'possible': 0.36; 'clear': 0.37; 'being': 0.38; 'awesome': 0.38; 'skip:. 20': 0.38; 'track': 0.38; 'itself': 0.39; 'skip:. 10': 0.39; 'most': 0.60; 'lost': 0.61; 'simple': 0.61; 'first': 0.61; 'name': 0.63; 'costs': 0.63; 'great': 0.65; 'within': 0.65; 'here': 0.66; '26,': 0.68; '2015': 0.84; 'cutting': 0.91; 'to:none': 0.92; 'package:': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=maH8v2ayuglJwwsRTWpeUkIFOupd0pju29OK04HHkxU=; b=HjwHJRD/J59IZjiuXuWm/QBXQ7poiVyvkUGjTjVycUpQFVqJ3gJPfRP8cdP9IifPmb 7wSq36vFKALaSFLalJRM/yTe71+ybwX2W+GLKaZDjGW4ygtWdGW90rHC8Z13Stoz7gHy g2RCitUk6+eLtUJz+G4TbzUHzsoJGLouvZEK3VojgFzle4Qv4zl+fvVjc5WJaDPULmh1 ZDv6sN6J15kXtPLLmIw9DqiNpAiXUSjm0Naxtq4pHe9uiWujnAJ4uHNBEJPV7fuZ8r/8 cVyluOtaNZ5TUupk2ZCDdYQvhUecPKUopyiRZfe+xX1al7TtBfXHTL4rJRNLRkEXtP/j kTjg== MIME-Version: 1.0 X-Received: by 10.107.16.32 with SMTP id y32mr5799949ioi.53.1430001976917; Sat, 25 Apr 2015 15:46:16 -0700 (PDT) In-Reply-To: References: Date: Sun, 26 Apr 2015 08:46:16 +1000 Subject: Re: Library import succeeds with nose, fails elsewhere From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430001979 news.xs4all.nl 2892 [2001:888:2000:d::a6]:38050 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89401 On Sun, Apr 26, 2015 at 3:48 AM, wrote: > Apologies, I'm a rubyist and this is a beginner question but I'm not finding a great answer with lots of googling. I am writing a library, organized something like this: > > awesome_lib/awesome.py > awesome_lib/util/__init__.py > awesome_lib/util/helper.py > > In the top of awesome.py: > > foo = 'bar' > import helper > > In the top of helper.py: > > import awesome > print awesome.foo > > IRL, I'm doing this for things like referring to the main logger from within the utility method. I've lost track of the util directory here; you don't seem to be mentioning it in your import anywhere. But I can recreate most of your setup as a simple package: $ grep -r $ . ./foo.py:import awesome ./foo.py:print("OKAY") ./awesome/helper.py:from . import foo ./awesome/helper.py:print("My foo is %s" % foo) ./awesome/__init__.py:foo = 'bar' ./awesome/__init__.py:from . import helper $ python2 foo.py My foo is bar OKAY $ python3 foo.py My foo is bar OKAY (I've tweaked your import and print syntax to make it Py3 compatible, since it costs so little.) The setup I'm using here is a straight-forward package. The first thing imported will always be __init__.py, and modules within the package (eg helper) can import from the package without problems, because it'll always be loaded. It's still a circular dependency, but it's a fairly clear and simple one. Note that I'm being explicit about the intra-package imports here. This is partly for Python 3 compatibility, but it's also for clarity; "from . import foo" cannot accidentally import from a shadowing module, but will only ever import a name from the package itself (it might be another module, "foo.py", or it might be a name assigned inside __init__.py, but it'll be from this package). When there's a problem with imports somewhere, cutting down possible shadowing is a great help with the debugging. ChrisA