[Python-Dev] Criticism of execfile() removal in Python3 (original) (raw)

Nick Coghlan ncoghlan at gmail.com
Tue Jun 10 09:36:02 CEST 2014


On 10 June 2014 12:23, Paul Sokolovsky <pmiscml at gmail.com> wrote:

1. It hampers interactive mode - instead of short and easy to type execfile("file.py") one needs to use exec(open("file.py").read()). I'm sure that's not going to bother a lot of people - after all, the easiest way to execute a Python file is to drop back to shell and restart python with file name, using all wonders of tab completion. But now imagine that Python interpreter runs on bare hardware, and its REPL is the only shell. That's exactly what we have with MicroPython's Cortex-M port. But it's not really MicroPython-specific, there's CPython port to baremetal either - http://www.pycorn.org/ .

https://docs.python.org/3/library/runpy.html#runpy.run_path

import runpy
file_globals = runpy.run_path("file.py")

The standard implementation of run_path reads the whole file into memory, but MicroPython would be free to optimise that and do statement by statement execution instead (while that will pose some challenges in terms of handling encoding cookies, future imports, etc correctly, it's certainly feasible).

Cheers, Nick.

-- Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia



More information about the Python-Dev mailing list