python-3.x - 如何在 Windows 10 上将代理配置到 GitBash 环境中
问题描述
我在 Windows 10 上安装了 Python 3.7.3,但由于我公司的 Internet 代理,我无法在 Gitbash (Git SCM) 中通过 PIP 安装 Python 包。
我尝试通过以下方式为代理创建环境变量,但没有成功:
- 导出 http_proxy='proxy.com:8080'
- 导出 https_proxy='proxy.com:8080'
我找到了一个适合我的临时解决方案:将以下别名插入到 .bashrc 文件中:
- 别名 python='winpty python.exe'
- 别名 pip='pip --proxy=proxy.com:8080'
以上方法有效,但我正在寻找一个更好的解决方案,这样我就不需要为我使用的每个命令设置别名。我正在考虑类似环境变量的东西,但还没有找到如何在 Windows 的 git bash 环境中设置它。
你知道怎么做吗?
解决方案
首先,你不需要 Git bash 到pip install
Python 模块。
只有 Git 路径(使用类似 linux 的命令,加上curl
,即使它在最近的 Windows 10 上可用),来自常规 CMD,简化PATH
:
set PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\
set GH=C:\path\to\git
set PATH=%GH%\bin;%GH%\usr\bin;%GH%\mingw64\bin;%PATH%
set PH=C:\path\to\Python37
set PATH=%PH%;%PH%\Scripts
其次,您可以pip install
在企业环境中,前提是:
- 您通过
px
HTTP 代理服务器访问您的代理,以通过 NTLM 代理自动进行身份验证 - 您使用并信任 pip 模块源的镜像替代品。
关于genotrance/px
,只需运行服务px --proxy=my.company.prpxy:<port> --save --config=/path/to/your/px.ini
无需输入您的凭据(Windows 日志/密码):代理使用您当前的登录会话。
设置您的环境变量以引用该代理:
set HTTP_PROXY=http://localhost:3128
set HTTPS_PROXY=http://localhost:3128
然后,这将起作用:
λ pip install -i http://pypi.mirror.frontiernet.net/simple --trusted-host pypi.mirror.frontiernet.net "ansible-tower-cli==3.2.1" --force-reinstall
Looking in indexes: http://pypi.mirror.frontiernet.net/simple
Collecting ansible-tower-cli==3.2.1
Downloading http://pypi.mirror.frontiernet.net/packages/98/39/566f2dc628917e28d6600607cd0a533b9ed02395297363b2db827e59e488/ansible-tower-cli-3.2.1.tar.gz (153kB)
100% |████████████████████████████████| 163kB 107kB/s
(使用您公司未阻止的任何 pypi 镜像=