mardi 5 juillet 2016

importing current package


I'm writing a script and running it with the -i argument in order to keep the repl open after it finishes in order to be able to see info about the objects that I'm using while debugging.

In order to avoid having to quit and re-run the script I figured out that I can modify and save the script and then import [script-name] and then call my main method with [script-name].[main-method] ().

Now I want to write a single-character method (for convenience) that does:

def x():
    import [this script]
    [this script].[main-method]()

but to also be able to change the script's file name and retain the easy-reloading functionality without having to alter the code.

I've tried using importlib (see below) to no avail.

def y():
   import importlib
   this = importlib.import_module("".join(sys.argv[0].split(".py"))) # sys.argv[0] gives [module-name].py and the rest of the code inside the parentheses removes the ".py"
   this.[main-method]()

Aucun commentaire:

Enregistrer un commentaire