Tag Archives: Python environment path

[Solved] os.py“, line 725, in __getitem__ raise KeyError(key) from None KeyError: ‘PATH‘

os.py”, line 725, in getitem raise KeyError(key) from None KeyError: ‘PATH’

scenario: when I was working on the project, I encountered a problem. Everything was running normally on the Linux server, but there was a problem when I was running in the remote finalshell (the code behind the process actually ran), but it seemed that there was a problem with the environment.


Solution process:

when I instantiated a class, I was a little puzzled by this problem. When I began to find out about os.environ [“path”], I knew that Python did not load the environment (here I used the virtual environment instead of the system default Python). So I went into the virtual environment to execute this: 

import os
print(os.environ["PATH"])

It is found that there is a value here, but the bug reported is none. Therefore, at the beginning of the code, I add the value printed out just now to the code, and then I can continue to execute Python code in the virtual environment of the server on the remote shell

import os
os.environ["PATH"] = "This string copies the value printed from the above code"

So far, everything can run normally