Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #106429
| From | Robin Becker <robin@reportlab.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | 2.7 source import in python 3.x |
| Date | 2016-04-04 11:27 +0100 |
| Message-ID | <mailman.7.1459765644.32530.python-list@python.org> (permalink) |
| References | <5702418D.6080906@chamonix.reportlab.co.uk> |
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
2.7 source import in python 3.x Robin Becker <robin@reportlab.com> - 2016-04-04 11:27 +0100
csiph-web