OS X Python 3.7 openssl 终极冲突解决大法(PART 2)

2024-02-04 14:18

本文主要是介绍OS X Python 3.7 openssl 终极冲突解决大法(PART 2),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

OS X Python 3.7 openssl 终极冲突解决大法(PART 2)


前文:OS X Python 3.7 openssl 终极冲突解决大法(PART 1)


继续思考问题

  继续昨天的思路,思考一个问题:官网下载的Python3.7安装后能够完美的使用openssl,而Homebrew安装的就不行,肯定是差了一些东西,既然已经安装了openssl,那么有可能是配置文件又问题?或者是系统路径设置问题?或者是缺少一些引用文件?

  一步步来找到问题所在,首先,看看Python导入SSL时有什么错误:

$ python3
Python 3.7.2 (default, Feb 12 2019, 08:16:11) 
[Clang 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 98, in <module>import _ssl             # if we can't import it, let the error propagate
ImportError: dlopen(/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/_ssl.cpython-37m-darwin.so, 2): Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylibReferenced from: /usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/_ssl.cpython-37m-darwin.soReason: image not found

  能确定的是缺少一些文件导致Python不能正确的使用SSL,那么,这些文件在哪儿?应该在哪儿?现在是否隐藏在那个路径中,是否需要在系统环境变量中添加这些路径?

  卸载Homebrew安装的openssl,再重新安装一遍。

$ brew install opensslUpdating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2q.sierra.bottl
Already downloaded: /Users/tianshuo/Library/Caches/Homebrew/downloads/b6dec2b57ee88a3cb02335284971b3b1e902d8b43fef115b456a971e5d0981ce--openssl-1.0.2q.sierra.bottle.tar.gz
==> Pouring openssl-1.0.2q.sierra.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in/usr/local/etc/openssl/certsand run/usr/local/opt/openssl/bin/c_rehashopenssl is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.If you need to have openssl first in your PATH run:echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profileFor compilers to find openssl you may need to set:export LDFLAGS="-L/usr/local/opt/openssl/lib"export CPPFLAGS="-I/usr/local/opt/openssl/include"==> Summary
?  /usr/local/Cellar/openssl/1.0.2q: 1,794 files, 12.3MB

  得到的提示和之前是一样的,事实也证明了,按brew的提示信息作link或者设置路径之类的工作没用,例如:(我的openssl安装路径是:/usr/local/Cellar/openssl/1.0.2q/)

$ brew link openssl
$ echo 'export PATH="/usr/local/Cellar/openssl/1.0.2q/bin:$PATH"' >> 
$ export LDFLAGS="-L/usr/local/Cellar/openssl/1.0.2q/lib"
$ export CPPFLAGS="-I/usr/local/Cellar/openssl/1.0.2q/include"

   看似进入了死胡同!

   上网搜!Bing,keyword:Homebrew openssl devel

  终于找到一篇文章!
   OpenSSL, OS X “El Capitan” and Brew

   经测试,完美解决问题。
   关键的两个步骤:

  • Step 1:
    建立文件的链接(替身),取代设置环境变量(原文:And now we’ll link it into our public area so you don’t have to figure out the magic environment variable to set while building your favorite OpenSSL-backed library:)
    将“/opt/openssl/”替换为实际安装路径,我的路径是:/usr/local/Cellar/openssl/1.0.2q/,那么下面代码中的"…/opt/openssl/“就应该是”…/Cellar/openssl/1.0.2q/"
$ cd /usr/local/include
$ ln -s ../opt/openssl/include/openssl .
  • Step 2:
    某些情况下,库文件路径也必须做修改,没有更好的办法,只能用笨方法,作文件的链接(原文:I have found in some cases that the library path must also be amended. I haven’t figured this one out yet in the general case, but this always works, even though it’s a mess:)
    将“/opt/openssl/”替换为实际安装路径,我的路径是:/usr/local/Cellar/openssl/1.0.2q/,那么下面代码中的"…/opt/openssl/“就应该是”…/Cellar/openssl/1.0.2q/"
$ cd /usr/local/lib
$ for i in ../opt/openssl/lib/lib*; do ln -vs $i .; done
./libcrypto.1.0.0.dylib -> ../opt/openssl/lib/libcrypto.1.0.0.dylib
./libcrypto.a -> ../opt/openssl/lib/libcrypto.a
./libcrypto.dylib -> ../opt/openssl/lib/libcrypto.dylib
./libssl.1.0.0.dylib -> ../opt/openssl/lib/libssl.1.0.0.dylib
./libssl.a -> ../opt/openssl/lib/libssl.a
./libssl.dylib -> ../opt/openssl/lib/libssl.dylib
  • 用之前的一段读取https网页的代码测试
$ python3
Python 3.7.2 (default, Feb 12 2019, 08:16:11) 
[Clang 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> from urllib.request import urlopen
>>> WORD_URL = "https://learncodethehardway.org/words.txt"
>>> WORDS = []
>>> for word in urlopen(WORD_URL).readlines():
...     WORDS.append(str(word.strip(), encoding="utf-8"))
... 
>>> print (WORDS)
['account', 'achiever', 'actor', 'addition', 'adjustment', 'advertisement', 'advice', 'aftermath', 'agreement', 'airplane', 'airport', 'alarm', 'amount', 'amusement', 'angle', 'animal', 'answer', 'ant', 'apparatus', 'apparel', 'apple', 'appliance', 'approval', 'arch', 'argument', 'arithmetic', 'arm', 'army', 'art', 'attack', 
# 下略... 

结论

这算不算是解决了问题呢?欢迎探讨。

这篇关于OS X Python 3.7 openssl 终极冲突解决大法(PART 2)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/677782

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

Python设置Cookie永不超时的详细指南

《Python设置Cookie永不超时的详细指南》Cookie是一种存储在用户浏览器中的小型数据片段,用于记录用户的登录状态、偏好设置等信息,下面小编就来和大家详细讲讲Python如何设置Cookie... 目录一、Cookie的作用与重要性二、Cookie过期的原因三、实现Cookie永不超时的方法(一)

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互