繁体   English   中英

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

[英]Using Python3 on macOS as default but pip still get using python 2.7

我正在使用 macOS Big Sur 11.0.1。

我正在设置一个virtual env环境

$python3 -m venv $my_workdir)/.virtualenv

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

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) ... /

由于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)

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

$python -V                                                                                                                                                                                                          
Python 3.8.2

然而,尝试升级 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

使用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)

而且我的virtualenv设置仍然导致同样的上述错误。

有人可以在这里说为什么pip仍在查看python 2.7站点包,以及wheel package 构建可能是什么问题?

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

export CPPFLAGS=-I/usr/local/opt/openssl/include
export LDFLAGS=-L/usr/local/opt/openssl/lib

但是还是不明白上述pip的行为。

我同意卢卡

  1. 使用brew install python
  2. 检查安装文件的位置ls -l /usr/local/bin/python
  3. ln -s -f /usr/local/bin/python3.7 /usr/local/bin/python
  4. 再次查看版本python --version

如果你的 Mac 上还没有安装 Homebrew,你可以在这里找到说明: https://docs.brew.sh/Installation

对于使用 Apple Silicon 的设备,您可以试试这个(因为默认的 homebrew bin 目录不同)

  export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include"

可以自己输入查看命令

brew info openssl

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM