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


Groups > comp.lang.python > #57892

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

From Peter Otten <__peter__@web.de>
Subject Re: Organising packages/modules - importing functions from a common.py in a separate directory?
Date 2013-10-29 08:44 +0100
Organization None
References (1 earlier) <mailman.1700.1382966048.18130.python-list@python.org> <7497d2bc-a42c-498b-9167-d86165401db8@googlegroups.com> <mailman.1734.1383008475.18130.python-list@python.org> <c178efff-259c-4823-85c4-864ce7534cdf@googlegroups.com> <ebf28078-c0a5-4ba5-ae95-59efe15c98e4@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.1746.1383032663.18130.python-list@python.org> (permalink)

Show all headers | View raw


Victor Hooi wrote:

> Hi,
> 
> Hmm, this post on SO seems to suggest that importing from another sibling
> directory in a package ins't actually possibly in Python without some ugly
> hacks?
> 
> http://stackoverflow.com/questions/6323860/sibling-package-imports
> 
> Did I read the above correctly?

Yes.
 
> Is there another way I can structure my code so that I can run the
> sync_em.py and sync_pg.py scripts, and they can pull common functions from
> somewhere?

The packages you are trying to access in your original post 

> foo_loading/
>     __init__.py
>     common/
>         common_foo.py
>     em_load/
>         __init__.py
>         config.yaml
>         sync_em.py
>     pg_load/
>         __init__.py
>         config.yaml
>         sync_pg.py


aren't actually siblings in the sense of the stackoverflow topic above, they 
are subpackages of foo_loading, and as you already found out

> So from within the sync_em.py script, I'm trying to import a function from 
foo_loading/common/common_foo.py.
> 
>     from ..common.common_foo import setup_foo_logging
> 
> I get the error:
> 
>     ValueError: Attempted relative import in non-package 
> 
> If I change directories to the parent of "foo_loading", then run
> 
>     python -m foo_loading.em_load.sync_em sync_em.py
> 
> it works. However, this seems a bit roundabout, and I suspect I'm not 
doing things correctly.
> 
> Ideally, I want a user to be able to just run sync_em.py from it's own 
directory, and have it correctly import the logging/config modules from 
common_foo.py, and just work.
> 
> What is the correct way to achieve this?

you can access them as long as the *parent* directory of foo_loading is in 
sys.path through PYTHONPATH, or as the working directory, or any other 
means. However, if you step into the package, e. g.

$ cd foo_loading
$ python -c 'import common'

then from Python's point of view 'common' is a toplevel package rather than 
the intended 'foo_loading.common', and intra-package imports will break.

To preserve your sanity I therefore recommend that you 

(1) avoid to put package directories into sys.path
(1a) avoid to cd into a package
(2) put scripts you plan to invoke directly rather than import outside the 
package.

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