Python |> Custom Environment for your Python Subprocess



IF YOU JUST SOLVED SOME PROBLEM, WELCOME TO DM qaRabbit ON TWITTER, OR CREATE A PULL REQUEST IN GITHUB REPO.

Yestorday, I found a script in build tools execute git without using my custom proxies.

After dug into the Python script, I had to find a way to set custom environment for subprocess.Popen or subprocess.check_call

import subprocess, os
proxy_env = os.environ.copy()
proxy_env["HTTP_PROXY"] = "http://host:port"
proxy_env["HTTPS_PROXY"] = "http://host:port"
subprocess.Popen(some_command, env=proxy_env)

Reference: