Search code examples
pythonwindowsenvironment-variablespython-osnssm

Getting os.environ to work with Python run via NSSM


I am stuck on an environment variables mismatch.

I run a Python script on Windows 10 via a program called NSSM.

At runtime, I do the following:

  1. Load in parameters from a text file
  2. Put its contents into the environment using os.environ.setdefault(name, value).
  3. Try to load in environment variables using os.environ[name]

Result:any variables I added do not show up.

I am not sure why the variables I add aren't available. Can you please tell me what I am doing wrong?

A starting point is that NSSM uses environment variables from Windows HKLM registry: source (see bottom). I am not sure if this is the reason os.environ cannot see relevant variables.


Solution

  • Well it turns out that my problem was outside of the scope of this question. @Recessive and @eryksun thank you both for answering, it put me "onto the scent".

    It turns out my problem was using Python pathlib's Path.home().

    • When running via command prompt, it pulled HOMEPATH environment variable.
    • When running via NSSM, it pulled USERPROFILE environment variable.

    This discrepancy in Path.home() was the real problem. It wasn't finding the environment variables because NSSM was looking in a totally different folder.