Today I Learn

Today I Learn

Snippets, Tips, Links

22 Jul 2020

Python |> Custom Environment for your Python Subprocess

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: