Python3 使用pip3報錯 ImportError: cannot import name 'HTTPSHandler' 解決
問題的起因:
我本來想裝一個pygame模組,但是使用pip系統預設裝到了python2下面,所以我想用pip3裝到python3吧版本上
然後使用pip3就出現了ImportError: cannot import name 'HTTPSHandler'錯誤,其原因就是安裝的python3版本不能使用ssl,
但是我的系統是有ssl的,搞了兩天最終解決.
問題示例:
[email protected]:~$ pip3 Traceback (most recent call last): File "/etc/python/python3.6/bin/pip3.6", line 9, in <module> load_entry_point('pip==8.0.2', 'console_scripts', 'pip3.6')() File "/etc/python/python3.6/lib/python3.6/site-packages/setuptools-19.6-py3.6.egg/pkg_resources/__init__.py", line 550, in load_entry_point File "/etc/python/python3.6/lib/python3.6/site-packages/setuptools-19.6-py3.6.egg/pkg_resources/__init__.py", line 2710, in load_entry_point File "/etc/python/python3.6/lib/python3.6/site-packages/setuptools-19.6-py3.6.egg/pkg_resources/__init__.py", line 2370, in load File "/etc/python/python3.6/lib/python3.6/site-packages/setuptools-19.6-py3.6.egg/pkg_resources/__init__.py", line 2376, in resolve File "/etc/python/python3.6/lib/python3.6/site-packages/pip-8.0.2-py3.6.egg/pip/__init__.py", line 15, in <module> from pip.vcs import git, mercurial, subversion, bazaar # noqa File "/etc/python/python3.6/lib/python3.6/site-packages/pip-8.0.2-py3.6.egg/pip/vcs/subversion.py", line 9, in <module> from pip.index import Link File "/etc/python/python3.6/lib/python3.6/site-packages/pip-8.0.2-py3.6.egg/pip/index.py", line 29, in <module> from pip.wheel import Wheel, wheel_ext File "/etc/python/python3.6/lib/python3.6/site-packages/pip-8.0.2-py3.6.egg/pip/wheel.py", line 39, in <module> from pip._vendor.distlib.scripts import ScriptMaker File "/etc/python/python3.6/lib/python3.6/site-packages/pip-8.0.2-py3.6.egg/pip/_vendor/distlib/scripts.py", line 14, in <module> from .compat import sysconfig, detect_encoding, ZipFile File "/etc/python/python3.6/lib/python3.6/site-packages/pip-8.0.2-py3.6.egg/pip/_vendor/distlib/compat.py", line 66, in <module> from urllib.request import (urlopen, urlretrieve, Request, url2pathname, ImportError: cannot import name 'HTTPSHandler'
問題查驗是不是因為ssl無法載入導致的。
1:先檢視系統是否有ssl 在終端輸入命令openssl
[email protected]:~$ openssl
OpenSSL>
OpenSSL>
OpenSSL>
OpenSSL>
OpenSSL> exit
[email protected]:~$
可以看到是可以用的。
2:進入python2,檢視是否可以使用ssl,終端輸入python預設進入python2的版本
[email protected]:~$ python
Python 2.7.10 (default, Aug 17 2018, 17:41:52)
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>>
>>> exit;
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> exit();
[email protected]:~$
可以看到匯入import ssl 沒有報錯
3:進入python3,檢視是否可以使用ssl,終端輸入python3預設進入python3.6的版本
[email protected]:~$ python3
Python 3.6.0 (default, Jun 19 2018, 13:24:21)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.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 "/etc/python/python3.6/lib/python3.6/ssl.py", line 101, in <module>
import _ssl # if we can't import it, let the error propagate
ModuleNotFoundError: No module named '_ssl'
>>>
>>> exit();
[email protected]:~$
很明顯報錯了沒有_ssl模組,這就說明我們安裝的python3沒有編譯好導致不能使用pip3安裝模組
解決辦法:
嘗試了非常多的方法都沒法解決,最後重新編譯安裝python3,後面的引數代表我們需要使用openssl
brew reinstall python3 --with-brewed-openssl
執行命令brew reinstall python3 --with-brewed-openssl
[email protected]:/usr/local/python3$ brew reinstall python3 --with-brewed-openssl
==> Reinstalling python
==> Installing dependencies for python: pkg-config, sphinx-doc, gdbm, openssl, readline and sqlite
==> Installing python dependency: pkg-config
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
Error: Failure while executing; `git config --local --replace-all homebrew.private true` exited with 1.
[email protected]:/usr/local/python3/Python-3.6.1$ brew reinstall /usr/local/python3/Python-3.6.1 --with-brewed-openssl
Error: No available formula with the name "/usr/local/python3/Python-3.6.1"
[email protected]:/usr/local/python3/Python-3.6.1$ brew reinstall python3 --with-brewed-openssl
==> Reinstalling python
==> Installing dependencies for python: pkg-config, sphinx-doc, gdbm, openssl, readline and sqlite
==> Installing python dependency: pkg-config
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
Error: Failure while executing; `git config --local --replace-all homebrew.private true` exited with 1.
報錯了,提取關鍵資訊(xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun)
翻譯:xcrun:錯誤:無效的活動開發者路徑(/Library/ developer /CommandLineTools),缺少xcrun在:/Library/ developer /CommandLineTools/usr/bin/xcrun
這又是什麼鬼,通過查閱資料找到:
1:先更新系統的Xcode
2:執行以下命令
$ xcode-select --install
安裝之後我們再次執行編譯命令,發現就可以安裝了
[email protected]:/usr/local/python3/Python-3.6.1$ brew reinstall python3 --with-brewed-openssl
==> Reinstalling python
==> Installing dependencies for python: gdbm, openssl, readline and sqlite
==> Installing python dependency: gdbm
==> Downloading https://homebrew.bintray.com/bottles/gdbm-1.18.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring gdbm-1.18.mojave.bottle.tar.gz
? /usr/local/Cellar/gdbm/1.18: 20 files, 588.7KB
==> Installing python dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2p.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.2p.mojave.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.2p: 1,793 files, 12MB
==> Installing python dependency: readline
==> Downloading https://homebrew.bintray.com/bottles/readline-7.0.5.mojave.bottle.tar.gz
######################################################################## 100.0%
模組都安裝好了最後安裝python3.7的時候報錯了
==> Installing python
==> Downloading https://homebrew.bintray.com/bottles/python-3.7.0.mojave.bottle.6.tar.gz
######################################################################## 100.0%
==> Pouring python-3.7.0.mojave.bottle.6.tar.gz
Error: An unexpected error occurred during the `brew link` step
The formula built, but is not symlinked into /usr/local
Permission denied @ dir_s_mkdir - /usr/local/Frameworks
Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks
翻譯:錯誤:在“brew link”步驟中發生了意外錯誤 構建的公式,但沒有符號連線到/usr/local 拒絕@ dir_s_mkdir - /usr/local/ framework的許可權 錯誤:拒絕@ dir_s_mkdir - /usr/local/Frameworks許可權
解決問題:
1:嘗試建立Frameworks資料夾
sudo mkdir /usr/local/Frameworks
2:更改目錄許可權
sudo chown -R $(whoami) /usr/local/Frameworks
成功後再次重新安裝python3
[email protected]:/usr/local/python3$ brew reinstall python3 --with-brewed-openssl
==> Reinstalling python
Warning: python: this formula has no --with-brewed-openssl option so it will be ignored!
==> Downloading https://homebrew.bintray.com/bottles/python-3.7.0.mojave.bottle.6.tar.gz
Already downloaded: /Users/xubin/Library/Caches/Homebrew/downloads/58056bc7bf880926324af3cc7e406c39b31b2876b74c6bb150de7b9318273b2f--python-3.7.0.mojave.bottle.6.tar.gz
==> Pouring python-3.7.0.mojave.bottle.6.tar.gz
==> /usr/local/Cellar/python/3.7.0/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/local/Cellar/python/3.7.0/bin --install-lib=/usr/local/lib/python3.7/site-packages --single
==> /usr/local/Cellar/python/3.7.0/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/local/Cellar/python/3.7.0/bin --install-lib=/usr/local/lib/python3.7/site-packages --single
==> /usr/local/Cellar/python/3.7.0/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/local/Cellar/python/3.7.0/bin --install-lib=/usr/local/lib/python3.7/site-packages --single
==> Caveats
Python has been installed as
/usr/local/bin/python3
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/usr/local/opt/python/libexec/bin
If you need Homebrew's Python 2.7 run
brew install [email protected]
Pip, setuptools, and wheel have been installed. To update them run
pip3 install --upgrade pip setuptools wheel
You can install Python packages with
pip3 install <package>
They will install into the site-package directory
/usr/local/lib/python3.7/site-packages
See: https://docs.brew.sh/Homebrew-and-Python
==> Summary
? /usr/local/Cellar/python/3.7.0: 4,781 files, 102MB
檢視一下python
[email protected]:~$ python
python python2.7 python3 python3.6 python3.6m python3.7 python3.7m pythonw
python-config python2.7-config python3-config python3.6-config python3.6m-config python3.7-config python3.7m-config pythonw2.7
發現系統有好多python,沒有用的刪掉就行了
驗證剛剛安裝的python3.7是否可用ssl
[email protected]:~$ python3.7
Python 3.7.0 (default, Oct 2 2018, 09:18:58)
[Clang 10.0.0 (clang-1000.11.45.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> exit();
好的沒有問題
最後看一下pip3是否可用
沒有問題,大功告成
特別感謝: