Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #106429 > unrolled thread
| Started by | Robin Becker <robin@reportlab.com> |
|---|---|
| First post | 2016-04-04 11:27 +0100 |
| Last post | 2016-04-04 11:27 +0100 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
2.7 source import in python 3.x Robin Becker <robin@reportlab.com> - 2016-04-04 11:27 +0100
| From | Robin Becker <robin@reportlab.com> |
|---|---|
| Date | 2016-04-04 11:27 +0100 |
| Subject | 2.7 source import in python 3.x |
| Message-ID | <mailman.7.1459765644.32530.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web