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


Groups > comp.lang.python > #60349

Re: Importing by file name

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 <jeanpierreda@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'interpreter': 0.05; 'finally:': 0.07; 'modify': 0.07; 'subject:file': 0.07; 'sys': 0.07; 'fname': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '23,': 0.16; 'happily': 0.16; 'knock': 0.16; 'pathname': 0.16; 'switch.': 0.16; 'sys.path:': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'import': 0.22; 'cc:addr:python.org': 0.22; 'script.': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'script': 0.25; 'post': 0.26; 'header:In- Reply-To:1': 0.27; 'chris': 0.29; 'absolute': 0.30; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'easier': 0.31; 'searches': 0.31; 'file': 0.32; 'probably': 0.32; 'skip:_ 10': 0.34; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'done': 0.36; 'being': 0.38; 'skip:o 20': 0.38; 'easiest': 0.38; 'nov': 0.38; 'pm,': 0.38; 'little': 0.38; 'success.': 0.39; 'sure': 0.39; 'different': 0.65; 'to:addr:gmail.com': 0.65; 'how.': 0.84; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=8nEb+nsHDAGsQT6XL1n4NGRNqb2d6/zN8ytzQKVERDs=; b=J7peDwSEhpc5J4Eeallwsk4NmaO9/4hpGMTLZ3eBTb1lpGprZhr+Z5ZbIevVgw0Fnn 2m0o92EwkK3CaILWSh1U2+DE3+yvHVJ287bba/R3aCgqqfK5lo1ORhSci+UCYFxlRnSu TQ/HEkOcLl4daAZWjzT7q04B2ZA8jHWpYc9T7m1ZFe5iWHuWzPr/dt6x9QHBJlywEsls 8RxXXwfpZx7CYLpS1Xd8u99sAucMHDFvc0GxKkIaFqxt0qjN3UstEqmvHrcrkjultSh7 avU9bZqadZL/JfiZ9oZbt/3FbtK0vGUqJ6xVhB9H1fy7Ud+RU2DT6wRKGeFoz9M3gmKv pv1w==
X-Received by 10.49.28.226 with SMTP id e2mr841052qeh.80.1385266042279; Sat, 23 Nov 2013 20:07:22 -0800 (PST)
MIME-Version 1.0
In-Reply-To <CAPTjJmqhiyT9zVHFRyKH0UwhdhFgZxA1rcbarChePMyfK7dDTA@mail.gmail.com>
References <CAPTjJmqhiyT9zVHFRyKH0UwhdhFgZxA1rcbarChePMyfK7dDTA@mail.gmail.com>
From Devin Jeanpierre <jeanpierreda@gmail.com>
Date Sat, 23 Nov 2013 20:06:42 -0800
Subject Re: Importing by file name
To Chris Angelico <rosuav@gmail.com>
Content-Type text/plain; charset=UTF-8
Cc "comp.lang.python" <python-list@python.org>
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.3115.1385266044.18130.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1385266044 news.xs4all.nl 15897 [2001:888:2000:d::a6]:51738
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:60349

Show key headers only | View raw


On Sat, Nov 23, 2013 at 7:41 PM, Chris Angelico <rosuav@gmail.com> wrote:
> As part of a post on python-ideas, I wanted to knock together a quick
> little script that "imports" a file based on its name, in the same way
> that the Python interpreter will happily take an absolute pathname for
> the main script. I'm sure there's a way to do it, but I don't know
> how. Obviously the import statement can't do it, but I've been poking
> around with __import__ and importlib without success. (Experiments are
> being done on Python 3.3; if a different version would make the job
> easier I'm happy to switch. This is just a curiosity tinkering.)

The easiest way is probably to modify what directories python searches
- sys.path:

import os
import sys

def file_import(path):
    dname, fname = os.path.split(path)
    modname, _ext = os.path.splitext(fname)
    sys.path.insert(0, dname)
    try:
        return __import__(modname)
    finally:
        del sys.path[0]

-- Devin

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


Thread

Re: Importing by file name Devin Jeanpierre <jeanpierreda@gmail.com> - 2013-11-23 20:06 -0800

csiph-web