Python : Accessing variables set via NppExec env_set



  • Is it possible to access environment variables set by the NppExec env_set command?
    So my env_set command looks like this (Which is executed at startup);
    env_set PROJECT=C:\MyProjects\Project1

    I am using os.getenv(“PROJECT”) but it isn’t returning a value. When i use os.getenv(“PATH”) i get a value.

    Is there a way to retrieve NppExec environment values or should i go about this another way?

    Thanks for any help.



  • @Anthony-Blinco said in Python : Accessing variables set via NppExec env_set:

    Is there a way to retrieve NppExec environment values or should i go about this another way?

    NppExec variables I don’t think so. Using NppExec to set system environment variables - I don’t think so either. Running Notepad++ inherits the default environment. If you open a Command Prompt or Powershell prompt, set a special environment variable (myvar) and then open Notepad++ from that command line, you’ll see myvar in NppExec:

    ce76d210-52ae-41b2-96c9-7e60fdf4cdad-image.png
    78863d16-b663-4539-bfdd-5fd98f165537-image.png

    However, that’s because NppExec is deriving its environment from Notepad++. If you then set environment variables in NppExec, they won’t be reflected back in that original Command Prompt you launched Notepad++ from. Nor will PythonScript - which is deriving its environment from Notepad++ - see any NppExec set environment variables. Maybe this screenshot from Process Explorer helps explain:

    cdaa5d1a-321d-4bdb-8669-5697b1bd4db2-image.png

    The cmd.exe under Notepad++ is me launching a CMD.exe process in NppExec console to have it “show up” in Process Explorer. You can see the hierarchy. Environment variable inheritance flows down, not up.

    Note, this is my “diagnosis” - this may not be fact, but is backed by my experimentation. Anyone who knows for sure feel free to correct me.

    Cheers.



  • @Anthony-Blinco said in Python : Accessing variables set via NppExec env_set:

    or should i go about this another way?

    Not sure at all what your end goal is, but to keep it simple you could probably “communicate” via files; you know NppExec writes to a file (presume it can do this?) and PythonScript looks for the file and reads it.

    There are more communication methods possible with Python if you need something “more complicated” but NppExec might be a limiting factor in what it can do (really no idea on that front as I’m not a NppExec user).



  • Ok thanks much. I appreciae it
    I’ll use files to communicate


Log in to reply