问题的起因:
我本来想装一个pygame模块,但是使用pip系统默认装到了python2下面,所以我想用pip3装到python3吧版本上
然后使用pip3就出现了ImportError: cannot import name 'HTTPSHandler'错误,其原因就是安装的python3版本不能使用ssl,
但是我的系统是有ssl的,搞了两天最终解决.
问题示例:
xubin@xubindeMBP:~$ 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
xubin@xubindeMBP:~$ openssl OpenSSL> OpenSSL> OpenSSL> OpenSSL> OpenSSL> exit xubin@xubindeMBP:~$可以看到是可以用的。
2:进入python2,查看是否可以使用ssl,终端输入python默认进入python2的版本
xubin@xubindeMBP:~$ 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(); xubin@xubindeMBP:~$可以看到导入import ssl 没有报错
3:进入python3,查看是否可以使用ssl,终端输入python3默认进入python3.6的版本
xubin@xubindeMBP:~$ 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(); xubin@xubindeMBP:~$很明显报错了没有_ssl模块,这就说明我们安装的python3没有编译好导致不能使用pip3安装模块
解决办法:
尝试了非常多的方法都没法解决,最后重新编译安装python3,后面的参数代表我们需要使用openssl
brew reinstall python3 --with-brewed-openssl执行命令brew reinstall python3 --with-brewed-openssl
xubin@xubindeMBP:/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. xubin@xubindeMBP:/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" xubin@xubindeMBP:/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安装之后我们再次执行编译命令,发现就可以安装了
xubin@xubindeMBP:/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/Frameworks2:更改目录权限
sudo chown -R $(whoami) /usr/local/Frameworks成功后再次重新安装python3
xubin@xubindeMBP:/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 python@2 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
xubin@xubindeMBP:~$ 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
xubin@xubindeMBP:~$ 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是否可用
没有问题,大功告成
特别感谢:
https://stackoverflow.com/questions/48908775/brew-install-python3-but-cant-link-to-python3
https://stackoverflow.com/questions/20688034/importerror-cannot-import-name-httpshandler-using-pip