In windows powershell terminal
. I can use $Env:http_proxy="http://127.0.0.1:7890";$Env:https_proxy="http://127.0.0.1:7890"
.
But I now want execute a python shell in windows powershell terminal
to do this. Can I do this? How to write the python script?
Fundamentally, a child process - which a Python script invoked from a PowerShell session invariably is - cannot modify its parent process' environment.
Your only option is to:
let the Python code output instructions for setting environment variables...
... which the PowerShell session must then act on.
For instance:
# Call the Python script (simulated here with a simple command that prints a
# variable-name-value pair) and
# split the output into variable name and value.
$name, $value =
(python -c 'print(''http_proxy=http://127.0.0.1:7890'')') -split '=', 2
# Set the environment variable accordingly.
Set-Item "Env:$Name" $value
# Test:
$env:http_proxy # -> 'http://127.0.0.1:7890'