我已经在我的本地机器上成功安装了Python 3.4和Python 3.6,但无法安装带有pip3的包。

当我执行pip3 install <package>时,我得到以下SSL相关错误:

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting <package>
  Could not fetch URL https://pypi.python.org/simple/<package>/: There was a problem confirming the ssl certificate: Can't connect to HTTPS URL because the SSL module is not available. - skipping
  Could not find a version that satisfies the requirement <package> (from versions: )
No matching distribution found for <package>

如何修复我的Python3。pip install <package>?


当前回答

同意mastaBlasta的回答。为我工作。我遇到了与主题描述相同的问题。

环境:MacOS Sierra。我用的是自制啤酒。

我的解决方案:

重新安装openssl安装openssl 根据Homebrew提供的提示,执行以下操作: echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile . bat 出口LDFLAGS =“- l / usr /地方/ opt / openssl / lib” 出口CPPFLAGS = " - i / usr /地方/ opt / openssl /包括“

其他回答

在使用pyenv来管理Mac OS Catalina上的python安装的情况下,我必须先用brew安装openssl,然后运行pyenv install 3.7.8,这似乎是使用homebrew中的openssl来构建python安装(它甚至在安装输出中这样说)。然后pyenv global 3.7.8,我离开了。

I finally solve this issue. These are the detail of my env: Version of Python to install: 3.6.8 OS: Ubuntu 16.04.6 LTS Root access: No Some people suggest to install libssl-dev, but it did not work for me. I follow this link and I fixed it! In short, I download, extract, build, and install OpenSSL (openssl-1.1.1b.tar.gz). Then, I modify .bashrc file follow this link. Next, I download and extract Python-3.6.8.tgz. I edit Modules/Setup.dist to modify SSL path (lines around #211). I did ./configure --prefix=$HOME/opt/python-3.6.8, make and make install. Last, I modify my .bashrc. Notice that I do not include --enable-optimizations in ./configure.

从源代码构建是我在Ubuntu 22.10上的工作方式:

手动安装OpenSSL,在这里用OpenSSL 1.1.1测试,提取然后运行:

./config --prefix='/opt/openssl' --openssldir='/opt/ssl'
make
make install

然后使用旧版本的Python 3(这里是Python-3.8.16)运行:

export LD_RUN_PATH='/opt/openssl/lib'
export CC='gcc-12'  # sudo apt install gcc-12
./configure --enable-optimizations \
            --with-openssl='/opt/openssl' \
            --prefix='/opt/python/3.8' -C
make
make install

测试:

/opt/python/3.8/bin/python3 -c 'import ssl; print(ssl.OPENSSL_VERSION)'
OpenSSL 1.1.1s  1 Nov 2022

ssl模块是用于访问操作系统(OS)套接字(Lib/ssl.py)的TLS/ ssl包装器。因此,当ssl模块不可用时,可能是您没有安装OS OpenSSL库,或者在安装Python时没有找到这些库。让我们假设这是后面的情况(也就是说:你已经安装了OpenSSL,但在安装Python时没有正确链接它们)。

我还假定您是从源代码安装的。如果你是从二进制文件(例如:windows .exe文件),或包(Mac .dmg,或Ubuntu apt)安装,在安装过程中你可以做的不多。

在配置python安装的步骤中,您需要指定将使用OS OpenSSL进行链接的位置:

# python 3.8 beta
./configure --with-openssl="your_OpenSSL root"

那么,在哪里可以找到已安装的OpenSSL目录呢?

# ubuntu 
locate ssl.h | grep '/openssl/ssl.h'

/home/user/.linuxbrew/Cellar/openssl/1.0.2r/include/openssl/ssl.h
/home/user/envs/py37/include/openssl/ssl.h
/home/user/miniconda3/envs/py38b3/include/openssl/ssl.h
/home/user/miniconda3/include/openssl/ssl.h
/home/user/miniconda3/pkgs/openssl-1.0.2s-h7b6447c_0/include/openssl/ssl.h
/home/user/miniconda3/pkgs/openssl-1.1.1b-h7b6447c_1/include/openssl/ssl.h
/home/user/miniconda3/pkgs/openssl-1.1.1c-h7b6447c_1/include/openssl/ssl.h
/usr/include/openssl/ssl.h

您的系统可能与我的不同,但正如您在这里看到的,我安装了许多不同的openssl库。在撰写本文时,python 3.8期望openssl 1.0.2或1.1:

Python要求使用X509_VERIFY_PARAM_set1_host()与OpenSSL 1.0.2或1.1兼容的libssl。

因此,您需要验证哪些已安装的库可以用于链接

/usr/bin/openssl version

OpenSSL 1.0.2g  1 Mar 2016
./configure --with-openssl="/usr"
make && make install

您可能需要尝试一些库,或者安装一个新的库,以找到适合您的Python和操作系统的库。

python文档实际上非常清楚,按照说明做了工作,而我在这里找到的其他答案并没有解决这个问题。

首先,安装python 3.x。例如3.6.2版本https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz 通过运行brew install openssl,确保已经安装了openssl tar xvzf python -3.6.2.tar。xz && cd Python-3.6.2 然后如果python版本< 3.7,则运行

CPPFLAGS="-I$(brew -prefix openssl)/include" \ LDFLAGS="-L$(brew -prefix openssl)/lib" \ . / configure——with-pydebug 5. 最后,运行make -s -j2 (-s是沉默标志,-j2告诉您的机器使用2个作业)