如何在PyCharm中指定pip选项以从requirements.txt安装 [英] How to specify pip options in PyCharm for installing from requirements.txt

查看:1373
本文介绍了如何在PyCharm中指定pip选项以从requirements.txt安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要从公司代理的后面安装Python软件包,有时必须向pip添加选项,例如--proxy--cert.

To install Python packages from behind a corporate proxy, it is sometimes necessary to add options to pip, such as --proxy or --cert.

如何在PyCharm中指定代理,请参见此问题以及如何添加任何pip调用的>选项在此答案中进行了说明.

How to specify a proxy in PyCharm is explained in this question and how to add any option to the pip call is explained in this answer.

后者将允许我添加必需的--cert选项.不幸的是,这仅在手动安装软件包时有效,并且不能解决我有requirements.txt文件并希望PyCharm自动安装列出的软件包的情况.这将导致此错误:

The latter would allow me to add the required --cert option. Unfortunately, this works only when installing a package manually and does not cover the case where I have a requirements.txt file and want PyCharm to automatically install the packages listed. This results in this error:

在连接被'SSLError(SSLError(SSL,(1,'[SSL:CERTIFICATE_VERIFY_FAILED])证书验证失败(_ssl) .c:833)'),)':/simple/robotframework/

Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)'),)': /simple/robotframework/

这意味着我需要向基础pip调用中添加一些内容(在本例中为--cert).

Which means that I need to add something to the underlying pip call (--cert in my case).

问题::如何指定PyCharm将用于自动安装requirements.txt中指定的软件包的pip选项?

Question: How can I specify pip options that will be used by PyCharm for automatic installation of the packages specified in requirements.txt?

推荐答案

您可以下载公司防火墙插入的CA证书,并将其安装到pip的密钥库中.下面是我使用的过程,但是我敢肯定那些更擅长证书格式/操作的人可以改善它:

You can download the CA cert inserted by your corporate firewall and install it to pip's keystore. Below is the process I used, but I'm sure those more adept at cert formats/manipulation can improve it:

请注意:每当pip时,pip的cacerts.pem文件都会被覆盖 已升级,因此需要重新插入公司证书.

PLEASE NOTE: Pip's cacerts.pem file will be overwritten whenever pip is upgraded, so the company cert will need to be re-inserted.

第1步.标识正确的密钥库.如果您使用的是虚拟环境,则pip激活时所使用的密钥库的位置应为C:\PATH\TO\VENV\Lib\site-packages\pip\_vendor\certifi\cacert.pem

Step 1. Identify the correct keystore. If you are using a virtual environment, the location of the keystore used by pip when it is activated should be C:\PATH\TO\VENV\Lib\site-packages\pip\_vendor\certifi\cacert.pem

注意:与我处理过的大多数密钥库不同(大多数是在试图使JetBrains产品在corpo防火墙后工作时),这是纯文本. 在第3步中对此有进一步的了解.

NOTE: Unlike most keystores that I have dealt with (mostly while trying to get JetBrains products to work behind corpo firewalls), this one is plain text. More on this in Step 3.

第2步.下载证书.使用Firefox(有许多方法可以执行此操作),请转到错误之前的URL(类似 https://pypi.org/simple/,或 https://pypi.python.org/simple/).单击锁定>显示连接详细信息>更多信息.在页面信息"窗口上,单击查看证书>详细信息选项卡.将顶级证书导出为.crt/.pem.单击返回到常规"选项卡,在步骤3中可能需要.

Step 2. Download the cert. Using Firefox (there are many ways to do this), go to the URL that precedes the error (something like https://pypi.org/simple/, or https://pypi.python.org/simple/). Click on the Lock > Show connection details > More information. On Page Info window, Click View Certificate > Details Tab. Export the top-level cert as .crt/.pem. Click back to the General tab, it may be needed in Step 3.

第3步.通常,您可以只使用keytool -import -alias key-alias -file "C:\path\to\exported\key.der" -keystore "C:\Path\to\keystore\.PyCharm2018.3\system\tasks\cacerts"之类的keytool命令,但是这样做时,会出现以下 keytool错误:java.security.KeyStoreException:无法识别的密钥库格式:null .事实证明,您只需将在第2步中导出的纯文本证书直接复制到密钥库中即可.

Step 3. Normally, you could just use a keytool command like keytool -import -alias key-alias -file "C:\path\to\exported\key.der" -keystore "C:\Path\to\keystore\.PyCharm2018.3\system\tasks\cacerts",but when you do, you get the following keytool error: java.security.KeyStoreException: Unrecognized keystore format: null. It turns out you can just copy the plain text cert exported in Step 2 directly into the keystore.

您不需要包括任何标头信息,只需从----- BEGIN CERTIFICATE -----到----- END CERTIFICATE -----.但是,将来如果有人(您)必须再次查看此密钥库,则可能会很有用,因此您可以从上述常规"选项卡中进行复制.

You don't need to include any header information, just from -----BEGIN CERTIFICATE----- to -----END CERTIFICATE-----. However, it may be beneficial in the future if someone (you) has to look at this keystore again, so you can copy it from the General Tab mentioned above.

这篇关于如何在PyCharm中指定pip选项以从requirements.txt安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆