OS X Python 3.7 openssl究極の衝突解決大法(PART 2)


OS X Python 3.7 openssl究極の衝突解決大法(PART 2)
前文:OS X Python 3.7 openssl究極衝突解決大法(PART 1)
問題を考え続ける
昨日の考え方を続けて、一つの問題を考えます:公式サイトでダウンロードしたPython 3.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 "", 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.dylib
  Referenced from: /usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/_ssl.cpython-37m-darwin.so
  Reason: image not found

PythonがSSLを正しく使用できないファイルがいくつか欠けていることが確認できます.では、これらのファイルはどこにありますか.どこにあるべきですか.そのパスに非表示にしますか?システム環境変数にパスを追加する必要がありますか?
Homebrewにインストールされているopensslをアンインストールし、再インストールします.
$ brew install openssl

Updating 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/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

openssl 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_profile

For 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.2 q/)
$ 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
テストを経て、問題を完璧に解決します.重要な2つのステップ:
  • 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.2 q/、次のコードの「.../opt/openssl/」は「はず」.../cellar/openssl/1.0.2 q/」
  • $ 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/cellar/openssl/1.0.2 q/,では、次のコードの「…/opt/openssl/」は「…/cellar/openssl/1.0.2 q/」
  • $ 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', 
    #   ... 
    

    結論
    これは問題を解決したのではないでしょうか.検討を歓迎します.