解决 Mac OS 编译 python 时的ssl依赖问题

系统版本: 10.11.6

问题:

直接编译安装python之后,使用时提示TLS/SSL不可用

$ /usr/local/python3/bin/pip3 install requests

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting requests

  Could not fetch URL https://pypi.python.org/simple/requests/: 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 requests (from versions: )
No matching distribution found for requests

解决思路:

查了config.log没有openssl模块依赖, 但是系统中是安装了openssl包的,which也能找到openssl命令,应该是编译python时没有检测到openssl

解决尝试

Requires

  • 涉及到编译需先安装 Xcode
$ brew install xcode
  • openssl依赖, 在重新安装和更新时提示了编译时依赖ssl的使用方法
$ brew install openssl # 或者 brew update openssl

If you need to have this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include
For pkg-config to find this software you may need to set:
    PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig

安装 python3 时,加入CFLAGS/LDFLAGS重新编译

$ wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz

$ tar Jxvf Python-3.6.2.tar.xz

$ cd Python-3.6.2

$ CFLAGS="-I$(brew --prefix openssl)/include" LDFLAGS="-L$(brew --prefix openssl)/lib" ./configure --prefix=/usr/local/python3

$ make && make install

按以上方法重新编译安装后,pip3已经可以正常下载其它软件包,问题解决

进一步python3虚拟空间环境的安装请看Centos7 中使用 virtualenv 管理 python3