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


Groups > comp.lang.python > #106432

Re: 2.7 source import in python 3.x

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: 2.7 source import in python 3.x
Date Mon, 04 Apr 2016 13:15:04 +0200
Organization None
Lines 62
Message-ID <mailman.10.1459768597.32530.python-list@python.org> (permalink)
References <5702418D.6080906@chamonix.reportlab.co.uk> <ndtibp$e5p$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de NWIzFHX9L0G8DWCG2k9GQg2bkGA2tDILY5nqNMvwGEag==
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; '"""': 0.05; '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; 'spec': 0.09; 'specifying': 0.09; 'subject:2.7': 0.09; 'python': 0.10; 'def': 0.13; 'subject:python': 0.14; 'instead.': 0.15; 'above)': 0.16; 'deprecation': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:import': 0.16; 'wrote:': 0.16; 'config': 0.18; 'helper': 0.18; 'stick': 0.18; 'try:': 0.18; 'load': 0.20; 'pass': 0.22; 'bit': 0.23; 'seems': 0.23; 'import': 0.24; 'module': 0.25; 'header:User-Agent:1': 0.26; 'example': 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; 'optional.': 0.29; 'quoting': 0.29; 'raise': 0.29; "i'm": 0.30; 'work.': 0.30; 'code': 0.30; 'skip:s 30': 0.31; 'url:python': 0.33; '(for': 0.34; 'file': 0.34; 'except': 0.34; 'ahead': 0.35; 'newer': 0.35; 'url:dev': 0.35; 'step': 0.36; 'skip:i 20': 0.36; 'there': 0.36; 'url:org': 0.36; 'actions': 0.36; 'url:library': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'version': 0.38; 'wrong': 0.38; 'anything': 0.38; 'sure': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'received:de': 0.40; 'some': 0.40; 'per': 0.62; '3.4': 0.84; 'becker': 0.84; 'subject:source': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd80fb.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
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 <ndtibp$e5p$1@ger.gmane.org>
X-Mailman-Original-References <5702418D.6080906@chamonix.reportlab.co.uk>
Xref csiph.com comp.lang.python:106432

Show key headers only | View raw


Robin Becker wrote:

> 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.

Seems like Python is one step ahead in the deprecation race. Quoting

https://docs.python.org/dev/library/importlib.html#importlib.machinery.SourceFileLoader.load_module

"""
load_module(name=None)
Concrete implementation of importlib.abc.Loader.load_module() where 
specifying the name of the module to load is optional.

Deprecated since version 3.6: Use importlib.abc.Loader.exec_module() 
instead.
"""

In the example section they have (for 3.4 and above)

spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)

The importlib with all its factories is a bit intimidating; perhaps it's 
best to stick with imp.load_source() and wait for some simple helper 
functions to appear...

> 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.
> 

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


Thread

Re: 2.7 source import in python 3.x Peter Otten <__peter__@web.de> - 2016-04-04 13:15 +0200

csiph-web