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


Groups > comp.lang.python > #106436

Re: 2.7 source import in python 3.x

From Robin Becker <robin@reportlab.com>
Newsgroups comp.lang.python
Subject Re: 2.7 source import in python 3.x
Date 2016-04-04 12:51 +0100
Message-ID <mailman.14.1459770692.32530.python-list@python.org> (permalink)
References <5702418D.6080906@chamonix.reportlab.co.uk> <ndtibp$e5p$1@ger.gmane.org> <5702553F.2070303@chamonix.reportlab.co.uk>

Show all headers | View raw


On 04/04/2016 12:15, Peter Otten wrote:
..........
>
> 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...
....

maybe two steps ahead :(

when I execute the example with python 3.4.2

> C:\code\hg-repos\reportlab>cat tmp\t1.py
> import importlib.util
> import sys
>
> # For illustrative purposes.
> import tokenize
> file_path = tokenize.__file__
> module_name = tokenize.__name__
>
> spec = importlib.util.spec_from_file_location(module_name, file_path)
> module = importlib.util.module_from_spec(spec)
> spec.loader.exec_module(module)
> # Optional; only necessary if you want to be able to import the module
> # by name later.
> sys.modules[module_name] = module
>
> C:\code\hg-repos\reportlab>\python34\python tmp\t1.py
> Traceback (most recent call last):
>   File "tmp\t1.py", line 10, in <module>
>     module = importlib.util.module_from_spec(spec)
> AttributeError: 'module' object has no attribute 'module_from_spec'

The example does work in python 3.5.0 so I guess the docs are a bit misleading.


-- 
Robin Becker

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


Thread

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

csiph-web