在 macOS 上默认使用 Python3,但 pip 仍然使用 python 2.7

nix*_*ind 7 python pip python-venv python-wheel

我使用的是 macOS Big Sur 11.0.1。

我正在设置一个virtual env

$python3 -m venv $my_workdir)/.virtualenv
Run Code Online (Sandbox Code Playgroud)

但在构建wheel包时出现此错误:

building '_openssl' extension
  creating build/temp.macosx-10.14.6-x86_64-3.8/build
  creating build/temp.macosx-10.14.6-x86_64-3.8/build/temp.macosx-10.14.6-x86_64-3.8
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -iwithsysroot/System/Library/Frameworks/System.framework/PrivateHeaders -iwithsysroot/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/Headers -arch arm64 -arch x86_64 -I/usr/local/opt/gettext/include -I/Users/engontang/devspace/energisme/terraform/tfwrapper-infra-pda/.wrapper/.virtualenv/include -I/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/include/python3.8 -c build/temp.macosx-10.14.6-x86_64-3.8/_openssl.c -o build/temp.macosx-10.14.6-x86_64-3.8/build/temp.macosx-10.14.6-x86_64-3.8/_openssl.o -Wconversion -Wno-error=sign-conversion
  build/temp.macosx-10.14.6-x86_64-3.8/_openssl.c:575:10: fatal error: 'openssl/opensslv.h' file not found
  #include <openssl/opensslv.h>
           ^~~~~~~~~~~~~~~~~~~~
  1 error generated.
  error: command 'clang' failed with exit status 1 

----------------------------------------

ERROR: Failed building wheel for cryptography

Building wheel for pynacl (PEP 517) ... /
Run Code Online (Sandbox Code Playgroud)

由于wheel是预先构建的,pip我认为这可能是pip升级问题,所以我检查了我的pip版本:

$pip --version                                                                                                                                                                                                      
pip 20.2b1 from /Library/Python/2.7/site-packages/pip-20.2b1-py2.7.egg/pip (python 2.7)
Run Code Online (Sandbox Code Playgroud)

我不明白为什么python 2.7上面仍然提到我的默认 python 版本是:

$python -V                                                                                                                                                                                                          
Python 3.8.2
Run Code Online (Sandbox Code Playgroud)

然而,尝试升级 pip 会导致以下结果:

$pip install --upgrade pip --user                                                                                                                                                                                   
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting pip
  Using cached pip-20.2.4-py2.py3-none-any.whl (1.5 MB)
Installing collected packages: pip
  WARNING: The scripts pip, pip2 and pip2.7 are installed in '/Users/engontang/Library/Python/2.7/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-20.2.4
Run Code Online (Sandbox Code Playgroud)

使用pip3命令给出相同的结果。您可以看到DEPRECATION: Python 2.7上面的消息,并且pip仍然显示相同的版本,而不是20.2.4

$pip --version                                                                                                                                                                                                       
pip 20.2b1 from /Library/Python/2.7/site-packages/pip-20.2b1-py2.7.egg/pip (python 2.7)
Run Code Online (Sandbox Code Playgroud)

我的virtualenv设置仍然导致上述相同的错误。

这里有人可以说一下为什么pip仍在查看站点包吗?包构建python 2.7可能存在什么问题?wheel

小智 20

对于使用 Apple Silicon 的设备,您可以尝试此操作(因为默认自制程序 bin 目录不同)

  export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include"
Run Code Online (Sandbox Code Playgroud)

可以自己输入查看命令

brew info openssl
Run Code Online (Sandbox Code Playgroud)

  • 如果您正在安装旧版本的“cryptography”(例如 2.9.x),您*必须*链接到“openssl@1.1”而不是“openssl@3”。 (2认同)

nix*_*ind 6

设置编译标志有助于解决opensslforwheel问题:

export CPPFLAGS=-I/usr/local/opt/openssl/include
export LDFLAGS=-L/usr/local/opt/openssl/lib
Run Code Online (Sandbox Code Playgroud)

但仍然不明白上述pip行为。

  • 不是作者,但 `brew info openssl` 将打印出 env 导出以运行以获取标志:) (3认同)