Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsfeed.tele2net.at!newsfeed.utanet.at!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.86.MISMATCH!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'package,': 0.03; 'importing': 0.05; 'tree': 0.05; 'subject:Python': 0.06; 'paths': 0.07; 'subject:file': 0.07; '22,': 0.09; 'subject:module': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'suggest': 0.14; '__init__.py': 0.16; 'ah,': 0.16; 'finney': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'paths.': 0.16; 'portable': 0.16; 'portable,': 0.16; 'subject: \n ': 0.16; 'subject:dependencies': 0.16; 'subject:import': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'module': 0.19; 'cc:addr:python.org': 0.22; 'specify': 0.24; 'looks': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'absolute': 0.30; 'relative': 0.30; 'message- id:@mail.gmail.com': 0.30; 'though.': 0.31; 'file': 0.32; 'running': 0.33; 'maybe': 0.34; 'subject:the': 0.34; "i'd": 0.34; 'subject:from': 0.34; 'could': 0.34; 'subject: (': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'module.': 0.36; "didn't": 0.36; 'method': 0.36; 'useful': 0.36; 'ben': 0.38; 'nov': 0.38; 'structure': 0.39; 'either': 0.39; "you're": 0.61; 'today': 0.64; 'more': 0.64; 'skip:\xe2 10': 0.65; 'life': 0.66; '8bit%:43': 0.74; '8bit%:46': 0.78; 'subject:history': 0.84; 'way)': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type:content-transfer-encoding; bh=ZkMt5e4nE0dZF1ADnZDhkO1NCOwAN8M/l0BtigkoEX0=; b=vFKGtz2ekYjo4UregbIGqQvMrwjZGdkgBj52PSUipPyM2ciJ1epahMTBclPUe5StcS ZiQeQF4h4Vy1sg3pm4+yVCKdhVPsQ037cigbJlAjo1wiHmyztkme+CR5C8+TR/LHL5jp p1koh13OBtEttlCHfvS82vyvavhoBOqx9XA7MGnZWchPMYXXckGgDIwZJWM31ovn6Smx 8M0+hY9gyrnJUIL9ogpG670UgM9wJdVUmNwVlcmY+oCqmDzszAHn09e57ibSxeR2EMzk yYW6OzP4vRbpgvamVJGVKhJXmzWACO8RcmodW5YuL/Og3lvxgLYN5VTCP3PqpSEyRMYM Tf5Q== MIME-Version: 1.0 X-Received: by 10.42.106.12 with SMTP id x12mr15817409ico.26.1416623529841; Fri, 21 Nov 2014 18:32:09 -0800 (PST) In-Reply-To: <85k32oysrg.fsf_-_@benfinney.id.au> References: <066c46c8-a04d-4489-b15a-b603c1b1818b@googlegroups.com> <8661e407-4472-4208-97c2-f666f6502c67@googlegroups.com> <0bbe1c60-2b29-40f6-96e1-ac777f0c276f@googlegroups.com> <08867178-abb6-4703-9ac5-2a53acb5d227@googlegroups.com> <85k32oysrg.fsf_-_@benfinney.id.au> Date: Sat, 22 Nov 2014 13:32:09 +1100 Subject: Re: Import a module from a specific file path (was: PyWart: "Python's import statement and the history of external dependencies") From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1416623533 news.xs4all.nl 2901 [2001:888:2000:d::a6]:41957 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81264 On Sat, Nov 22, 2014 at 11:37 AM, Ben Finney w= rote: > I don't think I'd ever want to specify an absolute file path for the > module. But it would make my Python life immeasurably better if I could > specify *relative* file paths for importing a module. > > Allowing relative paths makes this portable, so long as the > application's relative tree structure is maintained. > > Maybe you can suggest a better portable method to do this today in > Python. Ah, didn't think of relative paths. Yes, I can see that'd be both more useful and less problematic. What you suggest looks very much like you're running something from a package, though. > * The program =E2=80=98fooprog=E2=80=99 and the module =E2=80=98beans.py= =E2=80=99 are in a sensible > directory structure:: > > foo-proj-1.0/ > foo/ > __init__.py > fooprog > bar/ > __init__.py > beans.py > tests/ > __init__.py > test_fooprog.py > test_bar.py So can you simply: $ python -m foo.fooprog ? (or 'python3', either way) ChrisA