Conversion: execfile --> exec

Michael Selik michael.selik at gmail.com
Mon Jun 13 13:18:06 EDT 2016


On Mon, Jun 13, 2016, 10:36 AM Rustom Mody <rustompmody at gmail.com> wrote:

> On Monday, June 13, 2016 at 7:41:33 PM UTC+5:30, MRAB wrote:
> > On 2016-06-13 14:24, Long Yang wrote:
> > > The python 2.x command is as following:
> > > ---------------------------
> > > info = {}
> > > execfile(join('chaco', '__init__.py'), info)
> > > ------------------------------
> > >
> > > But execfile has been removed in python 3.x.
> > > So my problem is how to convert the above to a 3.x based command?
> > >
> > > thanks very much
> > >
> > Open the file and pass it to exec:
> >
> > info = {}
> > with open(join('chaco', '__init__.py')) as file:
> >      exec(file.read(), info)
>
>
> I wonder whether this should use importlib instead [yeah really
> wondering...
> not a rhetorical question]
>
> See slide 38-40 http://www.slideshare.net/pydanny/python-worst-practices


The slides you're referencing are saying importlib is better than exec'ing
an import. The question of this thread was more general. An import makes a
module object, but exec'ing arbitrary source does not (unless it uses
import).


More information about the Python-list mailing list