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


Groups > comp.lang.python > #106429

2.7 source import in python 3.x

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Robin Becker <robin@reportlab.com>
Newsgroups comp.lang.python
Subject 2.7 source import in python 3.x
Date Mon, 4 Apr 2016 11:27:25 +0100
Lines 36
Message-ID <mailman.7.1459765644.32530.python-list@python.org> (permalink)
References <5702418D.6080906@chamonix.reportlab.co.uk>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de Fz4RVOgGz+V0eGz/frriBQoTZlaWI4SO1B+D85F40LpA==
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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'deprecated': 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; 'skip:_ 80': 0.09; 'subject:2.7': 0.09; 'python': 0.10; 'def': 0.13; 'subject:python': 0.14; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subject:import': 0.16; 'config': 0.18; 'try:': 0.18; 'pass': 0.22; 'seems': 0.23; 'import': 0.24; 'module': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'skip:_ 20': 0.26; 'points': 0.27; 'skip:m 30': 0.27; 'function': 0.28; 'skip:( 20': 0.28; 'raise': 0.29; "i'm": 0.30; 'work.': 0.30; 'code': 0.30; 'file': 0.34; 'except': 0.34; 'newer': 0.35; 'skip:i 20': 0.36; 'there': 0.36; 'actions': 0.36; 'to:addr:python-list': 0.36; 'received:org': 0.37; 'wrong': 0.38; 'anything': 0.38; 'sure': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'per': 0.62; 'received:109': 0.75; 'becker': 0.84; 'subject:source': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host 109.174.168.73
X-Mozilla-News-Host news://news.gmane.org:119
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
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>
X-Mailman-Original-Message-ID <5702418D.6080906@chamonix.reportlab.co.uk>
Xref csiph.com comp.lang.python:106429

Show key headers only | View raw


A user points out that this code in reportlab uses the now deprecated imp module

def _fake_import(fn,name):
     if os.path.isfile(fn):
         import imp
         with open(fn,'rb') as f:
             imp.load_source(name,fn,f)

and suggests I use importlib SourceFileLoader. Is there anything wrong with this 
code (only for use in python 3.x)


def _fake_import(fn,name):
     from importlib import machinery
     m = machinery.SourceFileLoader(name,fn)
     try:
         return m.load_module(name)
     except FileNotFoundError:
         raise ImportError('file %s not found' % ascii(fn))

the newer import machinery seems particularly complex so I'm not at all sure 
this does what I intend even though it seems to work.

The original function is only used once like this

> try:
>     _fake_import(os.path.expanduser(os.path.join('~','.reportlab_mods')),'reportlab_mods')
> except (ImportError,KeyError):
>     pass

and is intended to allow per user actions in a config file when reportlab is 
imported.

-- 
Robin Becker

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


Thread

2.7 source import in python 3.x Robin Becker <robin@reportlab.com> - 2016-04-04 11:27 +0100

csiph-web