When an existing provisioning script needs to run pip through a proxy, launch pip as a subprocess. pip's internal Python functions are not a supported application API.

import subprocess
import sys

subprocess.run([
    sys.executable,
    '-m', 'pip', 'install',
    '--proxy', 'http://proxy.example:8080',
    'example-package',
], check=True)

Use the intended package and proxy endpoint. For public package downloads allowed through a public intermediary, the HTTP proxy list supplies candidates for the HTTP proxy argument. sys.executable selects the Python interpreter running the script, so this avoids accidentally invoking another installation's pip from PATH.

Each argument occupies a separate list element. No shell is needed, and the proxy URL does not need an extra pair of embedded shell quotes. check=True raises CalledProcessError if installation fails, allowing the provisioning workflow to handle that failure.

Configure the download route on this pip invocation or its subprocess environment. A Requests session configured elsewhere in the parent program does not configure pip's separate process.

After installation, continue the application's normal environment lifecycle. Installing into an interpreter that is already running does not guarantee that its current imports, caches or paths immediately reflect the new package.

Artiglyn mynjee