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

相关文章

Python实现终端清屏的几种方式详解

《Python实现终端清屏的几种方式详解》在使用Python进行终端交互式编程时,我们经常需要清空当前终端屏幕的内容,本文为大家整理了几种常见的实现方法,有需要的小伙伴可以参考下... 目录方法一:使用 `os` 模块调用系统命令方法二:使用 `subprocess` 模块执行命令方法三:打印多个换行符模拟

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

基于Python开发一个图像水印批量添加工具

《基于Python开发一个图像水印批量添加工具》在当今数字化内容爆炸式增长的时代,图像版权保护已成为创作者和企业的核心需求,本方案将详细介绍一个基于PythonPIL库的工业级图像水印解决方案,有需要... 目录一、系统架构设计1.1 整体处理流程1.2 类结构设计(扩展版本)二、核心算法深入解析2.1 自

从入门到进阶讲解Python自动化Playwright实战指南

《从入门到进阶讲解Python自动化Playwright实战指南》Playwright是针对Python语言的纯自动化工具,它可以通过单个API自动执行Chromium,Firefox和WebKit... 目录Playwright 简介核心优势安装步骤观点与案例结合Playwright 核心功能从零开始学习

Python 字典 (Dictionary)使用详解

《Python字典(Dictionary)使用详解》字典是python中最重要,最常用的数据结构之一,它提供了高效的键值对存储和查找能力,:本文主要介绍Python字典(Dictionary)... 目录字典1.基本特性2.创建字典3.访问元素4.修改字典5.删除元素6.字典遍历7.字典的高级特性默认字典

SpringBoot整合Dubbo+ZK注册失败的坑及解决

《SpringBoot整合Dubbo+ZK注册失败的坑及解决》使用Dubbo框架时,需在公共pom添加依赖,启动类加@EnableDubbo,实现类用@DubboService替代@Service,配... 目录1.先看下公共的pom(maven创建的pom工程)2.启动类上加@EnableDubbo3.实

MySQL逻辑删除与唯一索引冲突解决方案

《MySQL逻辑删除与唯一索引冲突解决方案》本文探讨MySQL逻辑删除与唯一索引冲突问题,提出四种解决方案:复合索引+时间戳、修改唯一字段、历史表、业务层校验,推荐方案1和方案3,适用于不同场景,感兴... 目录问题背景问题复现解决方案解决方案1.复合唯一索引 + 时间戳删除字段解决方案2:删除后修改唯一字

Python自动化批量重命名与整理文件系统

《Python自动化批量重命名与整理文件系统》这篇文章主要为大家详细介绍了如何使用Python实现一个强大的文件批量重命名与整理工具,帮助开发者自动化这一繁琐过程,有需要的小伙伴可以了解下... 目录简介环境准备项目功能概述代码详细解析1. 导入必要的库2. 配置参数设置3. 创建日志系统4. 安全文件名处

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处