There are various ways of re-enabling an execfile-like behaviour for Python 3.x environments - in the documentation and here on stackoverflow, but I did not find an exact replacement for my use case.
I am using IPython, and in Python 2.7.x execfile used to run a script file just as if I typed the exact same lines straight into IPython. This includes:
- useful exception-tracebacks are given
- local variables of my environment are available in the scripted code
- variables defined locally in the script are available in the environment (after the execfile call, of course)
import X as Y
statements in the script also make Y available in the environment- the execfile call works in interactive modes and also directly in python scripts
- execution of the entire script code is guaranteed for each call (except when hitting an exception)
- execfile is readily available wherever Python is - no lengthy definition or import of obscure package
Common solutions that have not entirely worked so far:
from scriptfile import *
does not satisfy #2 and #4. For function definitions, it also fails #6, as re-issuing the import does not update a function - this can be remedied with areload(scriptfile)
call.- The
exec(scriptfilehandle.read())
construction satisfies #5-7. With some amendments also #2-4 can be dealt with - but this evolves to a lengthy definition, which I can't recall right now, and tracebacks are still a mess. - IPython's
%run scriptfile
is nice, but falls short at least on requirement #2, #4 and #5. - Copying the script code from file and using IPython's
%paste
leaves out on #5 and #7 - and is quite cumbersome to do for each call.
Do you have any solutions I have not yet heard of?
I am using IPython+execfile while playing around with data, producing (lots of) matplotlib figures, trying stuff,... And if I like some lines I wrote, I put the code snippet in a script. Some examples of what I am doing:
- writing a script that prepares the environment for a specific dataset: doing imports, loading some data, defining some useful functions to work on this dataset,...
- semiautomatic plotting: elaborate script for beautifully plotting ten figures of data held in a local variable, then revising the plot-script, and re-executing it, then filtering the data, re-executing plot-script,...
- writing a script that utilizes several of my smaller snippets, to be run overnight on a large dataset
- apart from data exploration and plotting, sometimes I need to write small scripts on various systems: a RasPi, a router with OpenWRT, a machine without internet access, a Windows machine (without admin rights) - all of which may have their restrictions on what libraries are available
On the other hand I have to admit, that I'm not a professional programmer - my insight on Python's inner workings with local/global variables, and what really happens in an import
statement, are very limited.
Any help - may it be a solution to my problems or a helpful explaination - would be greatly appreciated!