Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #89396 > unrolled thread
| Started by | richmolj@gmail.com |
|---|---|
| First post | 2015-04-25 10:48 -0700 |
| Last post | 2015-04-26 06:40 -0700 |
| Articles | 7 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | richmolj@gmail.com |
|---|---|
| Date | 2015-04-25 10:48 -0700 |
| Subject | Library import succeeds with nose, fails elsewhere |
| Message-ID | <cd680312-ab2e-4303-aa20-7aa521a2f5fe@googlegroups.com> |
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. This works great when running tests through nose. In a test file I 'import awesome', refer to awesome.helper and everything is fine. If I try to put this library into a project, however, the import fails: ~/foo.py ~/awesome_lib 'python foo.py' (only code is 'from awesome_lib import awesome') ImportError: No module named awesome The error occurs when importing the helper and it tries to 'import awesome' and fails. I'm sure I am doing something stupid, can someone point me in the right direction?
[toc] | [next] | [standalone]
| From | alister <alister.nospam.ware@ntlworld.com> |
|---|---|
| Date | 2015-04-25 19:38 +0000 |
| Message-ID | <mhgqgf$2tc$1@speranza.aioe.org> |
| In reply to | #89396 |
On Sat, 25 Apr 2015 10:48:23 -0700, richmolj 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. > > This works great when running tests through nose. In a test file I > 'import awesome', refer to awesome.helper and everything is fine. > > If I try to put this library into a project, however, the import fails: > > ~/foo.py ~/awesome_lib > > 'python foo.py' (only code is 'from awesome_lib import awesome') > ImportError: No module named awesome > > The error occurs when importing the helper and it tries to 'import > awesome' and fails. > > I'm sure I am doing something stupid, can someone point me in the right > direction? you seem to have a recursive import going on Awesome imports helper, helper imports awesome this is always a cauise of problems & is usually a code smell. if these two modules are that heavily interlinked then you probably want all the code in one single module. -- All syllogisms have three parts, therefore this is not a syllogism.
[toc] | [prev] | [next] | [standalone]
| From | alister <alister.nospam.ware@ntlworld.com> |
|---|---|
| Date | 2015-04-25 19:38 +0000 |
| Message-ID | <mhgqfh$2t8$1@speranza.aioe.org> |
| In reply to | #89396 |
On Sat, 25 Apr 2015 10:48:23 -0700, richmolj 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. > > This works great when running tests through nose. In a test file I > 'import awesome', refer to awesome.helper and everything is fine. > > If I try to put this library into a project, however, the import fails: > > ~/foo.py ~/awesome_lib > > 'python foo.py' (only code is 'from awesome_lib import awesome') > ImportError: No module named awesome > > The error occurs when importing the helper and it tries to 'import > awesome' and fails. > > I'm sure I am doing something stupid, can someone point me in the right > direction? you seem to have a recursive import going on Awesome imports helper, helper imports awesome this is always a cauise of problems & is usually a code smell. if these two modules are that heavily interlinked then you probably want all the code in one single module. -- All syllogisms have three parts, therefore this is not a syllogism.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-04-26 08:46 +1000 |
| Message-ID | <mailman.19.1430001979.3680.python-list@python.org> |
| In reply to | #89396 |
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
[toc] | [prev] | [next] | [standalone]
| From | richmolj@gmail.com |
|---|---|
| Date | 2015-04-25 16:36 -0700 |
| Message-ID | <a890820d-e5cf-438d-ae54-4442a6eb4e70@googlegroups.com> |
| In reply to | #89401 |
Thanks, I appreciate the help. I did figure this out. Chris - apologies for the error in my example, it should have been 'import util' within awesome.py. IOW, I was looking for awesome.util.helper to be available, and I also wanted awesome.foo available within helper.py. The solution ended up being editing the top-level __init__.py: import awesome and then *when in a subdirectory*: import awesome_lib as awesome and *when in a different top-level file*: import awesome. IOW (from what I can tell) I made importing the package the same as importing this one file. When in a subdirectory import via the package, when in a sibling file import the file directly. This certainly feels odd, and I did find some (likely preferred) different ways I could handle it. My intent was that now I can refer to awesome.util.helper regardless of where I am (outside the package, within different directories, etc). My guess is that doing things like importing 'helper' directly and referring to it as 'helper' (no awesome.util prefix) is the python way of doing things, just didn't ring true with my background.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-04-26 09:53 +1000 |
| Message-ID | <mailman.20.1430005996.3680.python-list@python.org> |
| In reply to | #89405 |
On Sun, Apr 26, 2015 at 9:36 AM, <richmolj@gmail.com> wrote: > The solution ended up being editing the top-level __init__.py: > > import awesome > > and then *when in a subdirectory*: > > import awesome_lib as awesome > > and *when in a different top-level file*: > > import awesome. > > IOW (from what I can tell) I made importing the package the same as importing this one file. When in a subdirectory import via the package, when in a sibling file import the file directly. > > This certainly feels odd, and I did find some (likely preferred) different ways I could handle it. My intent was that now I can refer to awesome.util.helper regardless of where I am (outside the package, within different directories, etc). My guess is that doing things like importing 'helper' directly and referring to it as 'helper' (no awesome.util prefix) is the python way of doing things, just didn't ring true with my background. > Give "from . import helper" a try; you may find that it works just as well as "import helper" does, but more explicitly saying that you want it from the current package. Other than that, I think you have something that'll work fine. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | richmolj@gmail.com |
|---|---|
| Date | 2015-04-26 06:40 -0700 |
| Message-ID | <989ac24b-0572-44a5-b923-33b40e3295f4@googlegroups.com> |
| In reply to | #89406 |
On Saturday, April 25, 2015 at 7:53:27 PM UTC-4, Chris Angelico wrote: > On Sun, Apr 26, 2015 at 9:36 AM, <richmolj@gmail.com> wrote: > > The solution ended up being editing the top-level __init__.py: > > > > import awesome > > > > and then *when in a subdirectory*: > > > > import awesome_lib as awesome > > > > and *when in a different top-level file*: > > > > import awesome. > > > > IOW (from what I can tell) I made importing the package the same as importing this one file. When in a subdirectory import via the package, when in a sibling file import the file directly. > > > > This certainly feels odd, and I did find some (likely preferred) different ways I could handle it. My intent was that now I can refer to awesome.util.helper regardless of where I am (outside the package, within different directories, etc). My guess is that doing things like importing 'helper' directly and referring to it as 'helper' (no awesome.util prefix) is the python way of doing things, just didn't ring true with my background. > > > > Give "from . import helper" a try; you may find that it works just as > well as "import helper" does, but more explicitly saying that you want > it from the current package. Other than that, I think you have > something that'll work fine. > > ChrisA Just to follow up - could not get "from . import awesome" to work when in helper.py. Thanks for your help though.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web