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中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

MyBatis模糊查询报错:ParserException: not supported.pos 问题解决

《MyBatis模糊查询报错:ParserException:notsupported.pos问题解决》本文主要介绍了MyBatis模糊查询报错:ParserException:notsuppo... 目录问题描述问题根源错误SQL解析逻辑深层原因分析三种解决方案方案一:使用CONCAT函数(推荐)方案二:

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1