Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #89401
| References | <cd680312-ab2e-4303-aa20-7aa521a2f5fe@googlegroups.com> |
|---|---|
| Date | 2015-04-26 08:46 +1000 |
| Subject | Re: Library import succeeds with nose, fails elsewhere |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.19.1430001979.3680.python-list@python.org> (permalink) |
On Sun, Apr 26, 2015 at 3:48 AM, <richmolj@gmail.com> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Library import succeeds with nose, fails elsewhere richmolj@gmail.com - 2015-04-25 10:48 -0700
Re: Library import succeeds with nose, fails elsewhere alister <alister.nospam.ware@ntlworld.com> - 2015-04-25 19:38 +0000
Re: Library import succeeds with nose, fails elsewhere alister <alister.nospam.ware@ntlworld.com> - 2015-04-25 19:38 +0000
Re: Library import succeeds with nose, fails elsewhere Chris Angelico <rosuav@gmail.com> - 2015-04-26 08:46 +1000
Re: Library import succeeds with nose, fails elsewhere richmolj@gmail.com - 2015-04-25 16:36 -0700
Re: Library import succeeds with nose, fails elsewhere Chris Angelico <rosuav@gmail.com> - 2015-04-26 09:53 +1000
Re: Library import succeeds with nose, fails elsewhere richmolj@gmail.com - 2015-04-26 06:40 -0700
csiph-web