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


Groups > comp.lang.python > #109906

Re: Conversion: execfile --> exec

From Michael Selik <michael.selik@gmail.com>
Newsgroups comp.lang.python
Subject Re: Conversion: execfile --> exec
Date 2016-06-13 17:18 +0000
Message-ID <mailman.43.1465838297.2288.python-list@python.org> (permalink)
References <CAKyEaCuhNhRudhV_xMo6WGnePdjFvcZ=R2eDru7JjQUoHgB+tQ@mail.gmail.com> <b0590f59-7c17-3f69-f05a-6bfae53f8824@mrabarnett.plus.com> <mailman.40.1465827079.2288.python-list@python.org> <2ff66017-2c20-40eb-8e9d-853b33ec239b@googlegroups.com> <CAGgTfkPtvFgTEpgz=vKCrZtu2_KxozacLFzGsGK_E3LpfGu5+Q@mail.gmail.com>

Show all headers | View raw


On Mon, Jun 13, 2016, 10:36 AM Rustom Mody <rustompmody@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).

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


Thread

Re: Conversion: execfile --> exec MRAB <python@mrabarnett.plus.com> - 2016-06-13 15:11 +0100
  Re: Conversion: execfile --> exec Rustom Mody <rustompmody@gmail.com> - 2016-06-13 07:31 -0700
    Re: Conversion: execfile --> exec Michael Selik <michael.selik@gmail.com> - 2016-06-13 17:18 +0000
      Re: Conversion: execfile --> exec Rustom Mody <rustompmody@gmail.com> - 2016-06-13 10:45 -0700
        Re: Conversion: execfile --> exec Michael Selik <michael.selik@gmail.com> - 2016-06-13 22:14 +0000

csiph-web