Skip to main content
 首页 » 编程设计

python之在 OSX 上更新 Python 和包

2024年11月01日9dflying

我正在尝试更新运行 Mavericks 的 Apple 机器上的 python 安装。我担心我用自制软件搞砸了。

当我尝试使用制表符补全 python 时得到以下输出:

python            python2.5-config  python2.7         python3-config    pythonw           pythonw2.7 
python-config     python2.6         python2.7-config  python3.3         pythonw2.5        pythonw3.3 
python2.5         python2.6-config  python3           python3.3-config  pythonw2.6 

运行 where python 会产生以下输出 /usr/bin/python。类似地,运行 where python3 导致 /usr/local/bin/python3 和 where python3.3 导致 /usr/local/bin/python3.3.

当我分别运行每个不同的 python 可执行文件时,我得到以下输出:

$ python 
Python 2.7.5 (default, Aug 25 2013, 00:04:04) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> 
$ python3 
Python 3.3.5 (default, Mar 17 2014, 21:17:03) 
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.38)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> 
$ python3.3 
Python 3.3.5 (default, Mar 17 2014, 21:17:03) 
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.38)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> 

在 PyCharm 中,默认可用的解释器路径是:

  • /System/Library/Frameworks/Python.framework/Versions/2.5/bin/python
  • /System/Library/Frameworks/Python.framework/Versions/2.6/bin/python
  • /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python
  • /usr/local/bin/pypy
  • /usr/local/bin/python3
  • /usr/local/bin/python3.3

选择 /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python 作为我在 PyCharm 中的解释器显示我有几个包已经过时了。这些过时的包与我运行 pip list -o 时显示的相同:

Warning: cannot find svn location for setuptools==0.6c12dev-r88846 
bdist-mpkg (Current: 0.4.4 Latest: 0.5.0) 
Could not find any downloads that satisfy the requirement bonjour-py 
Some externally hosted files were ignored (use --allow-external bonjour-py to allow). 
matplotlib (Current: 1.1.1 Latest: 1.3.1) 
numpy (Current: 1.6.2 Latest: 1.8.0) 
pyOpenSSL (Current: 0.13 Latest: 0.14) 
python-dateutil (Current: 1.5 Latest: 2.2) 
pytz (Current: 2012d Latest: 2014.1) 
scipy (Current: 0.11.0 Latest: 0.13.3) 
setuptools (Current: 0.6c12dev-r88846 Latest: 3.3) 
Twisted (Current: 12.2.0 Latest: 13.2.0) 
xattr (Current: 0.6.4 Latest: 0.7.4) 
zope.interface (Current: 3.8.0 Latest: 4.1.0) 
pyobjc-core (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-AddressBook (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-AppleScriptKit (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-AppleScriptObjC (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-Automator (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-CFNetwork (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-Cocoa (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-Collaboration (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-CoreData (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-CoreLocation (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-CoreText (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-DictionaryServices (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-ExceptionHandling (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-FSEvents (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-InputMethodKit (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-InstallerPlugins (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-InstantMessage (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-InterfaceBuilderKit (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-LatentSemanticMapping (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-LaunchServices (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-Message (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-OpenDirectory (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-PreferencePanes (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-PubSub (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-QTKit (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-Quartz (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-ScreenSaver (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-ScriptingBridge (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-SearchKit (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-ServiceManagement (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-SyncServices (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-SystemConfiguration (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-WebKit (Current: 2.3.2a0 Latest: 2.5.1) 
pyobjc-framework-XgridFoundation (Current: 2.3.2a0 Latest: 2.5.1) 

尝试通过 PyCharm 更新 numpy 会导致以下错误:

Upgrade packages failed. 
 
 
The following command was executed: 
 
/Applications/PyCharm CE.app/helpers/packaging_tool.py install --build-dir /private/var/folders/7r/9rtxb7c94bv_k4szp8lnkpmm0000gn/T/pycharm-packaging258144586042338380.tmp -U numpy 
 
The error output of the command: 
 
Storing debug log for failure in /Users/myusername/Library/Logs/pip.log (1) 

检查日志文件,这似乎是导致失败的原因

clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future] 
clang: note: this will be a hard error (cannot be downgraded to a warning) in the future 
clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future] 
clang: note: this will be a hard error (cannot be downgraded to a warning) in the future 
failure. 

当我运行 sudo pip install -U numpy

时,出现与上述相同的错误

基于此信息,我(希望是正确的)得出结论,我正在尝试更新 Apple 提供的 python,而 MacPorts 可能更适合解决此任务。我安装了 MacPorts 并运行了 port upgrade outdated 结果是 Nothing to upgrade。

如何从 2.7.5 更新到 2.7.6 并更新相关包?

请您参考如下方法:

问题是特立独行者似乎已经破坏了 python 扩展的构建。 mavericks 上的 python 是使用具有旧选项的旧编译器构建的。这些选项存储在

中供以后使用
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_sysconfigdata.py 

您可以手动编辑此文件以删除错误的编译器选项,或者您可以让 sed 为您完成:

sed -i 's/-mno-fused-madd//' /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_sysconfigdata.py 

我只在系统 Python 2.7 上测试和使用过它。我不知道这是否适用于您的/usr/local python 版本。使用 homebrew 或其他工具用最新的编译器选项重建这些 python 实例可能更容易。

更新:您可能还更喜欢使用 gcc 而不是 clang。参见:https://stackoverflow.com/a/20295014/16998