Menu

Running a secondary script from the startup script.

Help
2015-10-22
2015-10-28
  • W P Blatchley

    W P Blatchley - 2015-10-22

    As I'm not much of a Pythoneer, I don't know the best way to do this, and certainly not in the context of PythonScript.

    How do I make startup.py call another script at the end, when it's finished doing its thing? I have written some code which registers a callback, and I want it to run when NPP starts, but I don't really want to pollute startup.py with my own code any more than I have to.

    So I just want a simple statement to add on the end, like: Execute("MyScript.py")

    Thanks in advance!

     
  • Dave W.

    Dave W. - 2015-10-22

    WP,
    Sure thing. Easy!
    The simplest way to do that is to just put the code you want to run in a separate file (a 'module', in Python terms), named e.g. "my_startup_code.py". Put that file right next to your default PythonScript startup.py module.
    Then, down at the end of startup.py, just put "import my_startup_code".
    Notice you don't specify the '.py' file extension this time. Python assumes that.
    The import statement, if successful, locates, loads, and runs all the code in your specified module.
    (You may need to re-specify the
    "import sys
    from Npp import *" statements as in startup.py. I don't remember exactly.)
    But that should do it.

    tl;dr ;)

    As I'm not much of a Pythoneer, I don't know the best way to do this, and certainly not in the context of PythonScript.

    Bear with me as I attempt to address what I suspect may be a mis-conception...
    There really is no special "context of PythonScript", other than the additional Npp objects it provides. And those are just plain old Python objects.
    The great thing about PythonScript is, it's just Python, artfully integrated into Npp itself as a plugin. It's not a subset or a special dialect or syntax or "yet another Python-like scripting language"... it's just... Python. ;)
    So, for the most part, just about anything you learn about Python is applicable to "PythonScript".
    Yeah, there may be a few little obscure gotchas, but... you'll probably never hit 'em so no point worrying about it. ;)

    Hope that helps.
    Have good.

     
  • W P Blatchley

    W P Blatchley - 2015-10-28

    Thanks very much for explaining that. Not unlike a C preprocessor #include. I suppose I didn't think of something along those lines because I think of Python as being interpreted rather than compiled. Of course, I have a feeling it is compiled to a byte-code, and anyway, I suppose there's no reason why you can't "interpret" an "include". Simple solution - many thanks!

     

Log in to post a comment.