Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80571
| Path | csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!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; 'python.': 0.02; 'languages,': 0.04; 'explicitly': 0.05; 'subject:Python': 0.06; 'level,': 0.07; 'imported': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'separately': 0.09; 'stating': 0.09; 'structure,': 0.09; 'subject:modules': 0.09; 'python': 0.11; '__init__.py': 0.16; 'filesystem': 0.16; 'hierarchy': 0.16; 'hierarchy.': 0.16; 'imports': 0.16; 'itself,': 0.16; 'locations.': 0.16; 'path.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reminding': 0.16; 'top-level': 0.16; 'tripped': 0.16; 'module': 0.19; 'trying': 0.19; "python's": 0.19; 'import': 0.22; 'header:User-Agent:1': 0.23; 'package.': 0.24; 'specify': 0.24; 'helpful': 0.24; 'looks': 0.24; 'header:X -Complaints-To:1': 0.27; 'point': 0.28; 'besides': 0.30; 'relative': 0.30; 'work.': 0.31; 'writes:': 0.31; 'allows': 0.31; '(e.g.': 0.33; 'trouble': 0.34; "can't": 0.35; 'but': 0.35; 'there': 0.35; 'executing': 0.36; 'project': 0.37; 'starting': 0.37; 'system,': 0.38; 'to:addr:python-list': 0.38; 'structure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'skip:* 10': 0.61; 'simple': 0.61; 'myself': 0.63; 'skip:\xe2 10': 0.65; 'note:': 0.66; 'between': 0.67; 'of:': 0.68; '8bit%:43': 0.74; "project's": 0.84; 'received:125': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Ben Finney <ben+python@benfinney.id.au> |
| Subject | Re: Python modules |
| Date | Wed, 12 Nov 2014 09:18:50 +1100 |
| References | <i0h06a9pj8h3olo5rnrgc64i7ckpvtvti6@4ax.com> <e59e1214-b867-4e7b-9d31-fb8c2efd5d40@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8 |
| Content-Transfer-Encoding | 8bit |
| X-Gmane-NNTP-Posting-Host | jigong.madmonks.org |
| X-Public-Key-ID | 0xAC128405 |
| X-Public-Key-Fingerprint | 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 |
| X-Public-Key-URL | http://www.benfinney.id.au/contact/bfinney-pubkey.asc |
| X-Post-From | Ben Finney <bignose+hates-spam@benfinney.id.au> |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) |
| Cancel-Lock | sha1:Cjh6nqeQmv8DdsmVZMT1AYi0MKc= |
| 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.15696.1415744348.18130.python-list@python.org> (permalink) |
| Lines | 61 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1415744348 news.xs4all.nl 2941 [2001:888:2000:d::a6]:35385 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:80571 |
Show key headers only | View raw
Will Acheson <willyach07@gmail.com> writes:
> I have had a lot of trouble with executing relative imports with some
> of my projects in python.
>
> Are there any best practices or methods besides '../../' type
> hard-coding?
The important point to learn with Python's import system, as contrasted
with various other languages, is that it is *intentionally* abstracted
from the filesystem hierarchy.
That allows all kinds of helpful infrastructure, but it also comes with
the cost of learning how to let Python know where modules are to be
imported from; it's not a simple case of stating filesystem locations.
The point which tripped me up for a long time, and which I have to keep
reminding myself of:
Python looks for modules in its import search path. Corollary: structure
your project into a hierarchy of packages, including the top level, and
specify all relative imports starting at that top-level package.
Often the cause of my intra-project import woes comes from having
several *discrete* packages, but trying to do relative imports between
them::
hurgen/ # Note: ‘hurgen’ is not a Python package.
foo/
__init__.py
spam.py
beans.py
bar/
__init__.py
eggs.py
ham.py
This is a poor structure, because Python needs to be told about each of
‘foo’ and ‘bar’ separately if relative imports are to work. Worse, a
module invoked as the top-level module (e.g. ‘eggs.py’) can't do imports
relative to itself, because it isn't even aware it's in a package!
Better is to structure the project explicitly under a top-level named
Python package::
hurgen/
__init__.py
foo/
__init__.py
spam.py
beans.py
bar/
__init__.py
eggs.py
ham.py
and import all the project's modules relative to the ‘hurgen’ package.
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Python modules Steve Hayes <hayesstw@telkomsa.net> - 2014-11-10 06:55 +0200
Re: Python modules Deepfriedice <Deepfriedice@openmailbox.org> - 2014-11-10 14:55 +1000
Re: Python modules Ben Finney <ben+python@benfinney.id.au> - 2014-11-10 16:12 +1100
Re: Python modules Steve Hayes <hayesstw@telkomsa.net> - 2014-11-10 08:58 +0200
Re: Python modules Ben Finney <ben+python@benfinney.id.au> - 2014-11-10 18:48 +1100
Re: Python modules Roy Smith <roy@panix.com> - 2014-11-10 08:36 -0500
Re: Python modules Chris Angelico <rosuav@gmail.com> - 2014-11-11 00:49 +1100
Re: Python modules Will Acheson <willyach07@gmail.com> - 2014-11-11 12:06 -0800
Re: Python modules Ben Finney <ben+python@benfinney.id.au> - 2014-11-12 09:18 +1100
csiph-web