0

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:

  1. useful exception-tracebacks are given
  2. local variables of my environment are available in the scripted code
  3. variables defined locally in the script are available in the environment (after the execfile call, of course)
  4. import X as Y statements in the script also make Y available in the environment
  5. the execfile call works in interactive modes and also directly in python scripts
  6. execution of the entire script code is guaranteed for each call (except when hitting an exception)
  7. 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 a reload(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!

  • https://stackoverflow.com/questions/47348860/save-params-between-script-runs-in-ipython-console seeks a `spyder` like behavior. – hpaulj Nov 24 '17 at 21:18
  • From 2009, a similar question seeking an execfile replacement: https://stackoverflow.com/questions/436198/what-is-an-alternative-to-execfile-in-python-3 – hpaulj Nov 24 '17 at 21:21
  • @hpaulj I know that there are a few similar questions - but I did not find any answer that delivers a true replacement. In my question I commented on the workaround drom selected answer in the question you linked. It just does not really work. – NightLightFighter Nov 25 '17 at 11:59
  • @hpaulj I looked into the sypder link you posted... the described behaviour (saving and reloading entire sessions) is not what I am looking for. But over there you commented on using the `-i` flag for IPython's `%run` - I'll look into that - thanks! This still won't be helpful in pure python scripts or embedded environments, but it may be super useful in my day-to-day application. – NightLightFighter Nov 25 '17 at 12:04

0 Answers0