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调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

Goland debug失效详细解决步骤(合集)

《Golanddebug失效详细解决步骤(合集)》今天用Goland开发时,打断点,以debug方式运行,发现程序并没有断住,程序跳过了断点,直接运行结束,网上搜寻了大量文章,最后得以解决,特此在这... 目录Bug:Goland debug失效详细解决步骤【合集】情况一:Go或Goland架构不对情况二: