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


Groups > comp.lang.python > #57867

Re: Organising packages/modules - importing functions from a common.py in a separate directory?

Newsgroups comp.lang.python
Date 2013-10-28 18:08 -0700
References <9283f879-b872-43af-a072-0dff088421d8@googlegroups.com> <mailman.1700.1382966048.18130.python-list@python.org> <7497d2bc-a42c-498b-9167-d86165401db8@googlegroups.com> <mailman.1734.1383008475.18130.python-list@python.org>
Message-ID <c178efff-259c-4823-85c4-864ce7534cdf@googlegroups.com> (permalink)
Subject Re: Organising packages/modules - importing functions from a common.py in a separate directory?
From Victor Hooi <victorhooi@gmail.com>

Show all headers | View raw


Hi,

If I try to use:

    from .common.common_foo import setup_foo_logging

I get:

    ValueError: Attempted relative import in non-package

And the absolute imports don't seem to be able to find the right modules.

Is it something to do with the fact I'm running the sync_em.py script from the "foo_loading/em_load" directory?

I thought I could just refer to the full path, and it'd find it, but evidently not...hmm.

Cheers,
Victor

On Tuesday, 29 October 2013 12:01:03 UTC+11, Ben Finney  wrote:
> Victor Hooi <victorhooi@gmail.com> writes:
> 
> 
> 
> > Ok, so I should be using absolute imports, not relative imports.
> 
> 
> 
> I'd say it is fine to use relative imports, so long as they are
> 
> explicit. (In Python 3, the default for an import is to be absolute, and
> 
> the *only* way to do a relative import is to make it explicitly
> 
> relative. So you may as well start doing so now.)
> 
> 
> 
> > Hmm, I just tried to use absolute imports, and it can't seem to locate
> 
> > the modules:
> 
> >
> 
> > In the file "foo_loading/em_load/sync_em.py", I have:
> 
> >
> 
> >     from common.common_bex import setup_foo_logging
> 
> 
> 
> So I'd recommend this be done with an explicit relative import:
> 
> 
> 
>     from .common.common_bex import setup_foo_logging
> 
> 
> 
> or, better, import a module:
> 
> 
> 
>     from .common import common_bex
> 
> 
> 
> or a whole package:
> 
> 
> 
>     from . import common
> 
> 
> 
> -- 
> 
>  \         “I went over to the neighbor's and asked to borrow a cup of |
> 
>   `\       salt. ‘What are you making?’ ‘A salt lick.’” —Steven Wright |
> 
> _o__)                                                                  |
> 
> Ben Finney

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


Thread

Re: Organising packages/modules - importing functions from a common.py in a separate directory? Jean-Michel Pichavant <jeanmichel@sequans.com> - 2013-10-28 14:12 +0100
  Re: Organising packages/modules - importing functions from a common.py in a separate directory? Victor Hooi <victorhooi@gmail.com> - 2013-10-28 17:51 -0700
    Re: Organising packages/modules - importing functions from a common.py in a separate directory? Ben Finney <ben+python@benfinney.id.au> - 2013-10-29 12:01 +1100
      Re: Organising packages/modules - importing functions from a common.py in a separate directory? Victor Hooi <victorhooi@gmail.com> - 2013-10-28 18:08 -0700
        Re: Organising packages/modules - importing functions from a common.py in a separate directory? Victor Hooi <victorhooi@gmail.com> - 2013-10-28 23:58 -0700
          Re: Organising packages/modules - importing functions from a common.py in a separate directory? Peter Otten <__peter__@web.de> - 2013-10-29 08:44 +0100
            Re: Organising packages/modules - importing functions from a common.py in a separate directory? Victor Hooi <victorhooi@gmail.com> - 2013-10-29 12:39 -0700
              Re: Organising packages/modules - importing functions from a common.py in a separate directory? Peter Otten <__peter__@web.de> - 2013-10-30 10:48 +0100
              Re: Organising packages/modules - importing functions from a common.py in a separate directory? Peter Otten <__peter__@web.de> - 2013-10-30 11:23 +0100

csiph-web