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


Groups > comp.lang.python > #109902 > unrolled thread

Re: Conversion: execfile --> exec

Started byMRAB <python@mrabarnett.plus.com>
First post2016-06-13 15:11 +0100
Last post2016-06-13 22:14 +0000
Articles 5 — 3 participants

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.


Contents

  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

#109902 — Re: Conversion: execfile --> exec

FromMRAB <python@mrabarnett.plus.com>
Date2016-06-13 15:11 +0100
SubjectRe: Conversion: execfile --> exec
Message-ID<mailman.40.1465827079.2288.python-list@python.org>
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)

[toc] | [next] | [standalone]


#109903

FromRustom Mody <rustompmody@gmail.com>
Date2016-06-13 07:31 -0700
Message-ID<2ff66017-2c20-40eb-8e9d-853b33ec239b@googlegroups.com>
In reply to#109902
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

[toc] | [prev] | [next] | [standalone]


#109906

FromMichael Selik <michael.selik@gmail.com>
Date2016-06-13 17:18 +0000
Message-ID<mailman.43.1465838297.2288.python-list@python.org>
In reply to#109903
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).

[toc] | [prev] | [next] | [standalone]


#109908

FromRustom Mody <rustompmody@gmail.com>
Date2016-06-13 10:45 -0700
Message-ID<9f4457b3-4f31-4285-ad66-eb4db87b8dd9@googlegroups.com>
In reply to#109906
On Monday, June 13, 2016 at 10:48:33 PM UTC+5:30, Michael Selik wrote:
> On Mon, Jun 13, 2016, 10:36 AM Rustom Mody  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).


True but the supplied code:
info = {}
execfile(join('chaco', '__init__.py'), info) 

looks (to me) like an intent to import the package chaco with no locals and globals -- Just guessing of course

[toc] | [prev] | [next] | [standalone]


#109910

FromMichael Selik <michael.selik@gmail.com>
Date2016-06-13 22:14 +0000
Message-ID<mailman.45.1465856071.2288.python-list@python.org>
In reply to#109908
On Mon, Jun 13, 2016 at 1:51 PM Rustom Mody <rustompmody@gmail.com> wrote:

> looks (to me) like an intent to import the package chaco with no locals
> and globals -- Just guessing of course
>

And without creating a module object. I suppose that means it doesn't get
cached in sys.modules either. Not sure if that's a feature or a bug.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web