Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #57892
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| 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; 'scripts': 0.03; 'package,': 0.03; 'importing': 0.05; 'error:': 0.07; 'ugly': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sanity': 0.09; 'scripts,': 0.09; 'subject:modules': 0.09; 'toplevel': 0.09; 'valueerror:': 0.09; 'works.': 0.09; 'python': 0.11; 'suggest': 0.14; '__init__.py': 0.16; 'attempted': 0.16; 'ideally,': 0.16; 'imports': 0.16; 'means.': 0.16; 'preserve': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'script,': 0.16; 'sys.path': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'trying': 0.19; "python's": 0.19; 'skip:f 30': 0.19; 'seems': 0.21; 'import': 0.22; 'this?': 0.23; 'header:User- Agent:1': 0.23; "aren't": 0.24; 'package.': 0.24; 'possibly': 0.26; 'post': 0.26; 'subject:/': 0.26; 'header:X-Complaints-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'correct': 0.29; 'topic': 0.29; 'relative': 0.30; "i'm": 0.30; 'work.': 0.31; 'code': 0.31; 'correctly.': 0.31; 'directory,': 0.31; 'invoke': 0.31; 'yes.': 0.31; 'run': 0.32; 'another': 0.32; 'sense': 0.34; 'subject:from': 0.34; 'common': 0.35; '(2)': 0.35; 'there': 0.35; 'doing': 0.36; 'subject:?': 0.36; 'hi,': 0.36; 'step': 0.37; 'to:addr:python- list': 0.38; 'rather': 0.38; 'skip:. 10': 0.39; 'structure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'read': 0.60; 'above,': 0.60; 'within': 0.65; 'therefore': 0.72; 'break.': 0.84; 'victor': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Organising packages/modules - importing functions from a common.py in a separate directory? |
| Date | Tue, 29 Oct 2013 08:44:47 +0100 |
| Organization | None |
| 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> <c178efff-259c-4823-85c4-864ce7534cdf@googlegroups.com> <ebf28078-c0a5-4ba5-ae95-59efe15c98e4@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p5084a9ae.dip0.t-ipconnect.de |
| User-Agent | KNode/4.7.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1746.1383032663.18130.python-list@python.org> (permalink) |
| Lines | 76 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1383032663 news.xs4all.nl 16008 [2001:888:2000:d::a6]:59189 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:57892 |
Show key headers only | 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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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