在OSX上更新Python及其包

2 投票
1 回答
1772 浏览
提问于 2025-04-17 22:46

我正在尝试在一台运行Mavericks系统的苹果电脑上更新Python的安装。我担心我在使用Homebrew的时候搞砸了。

当我尝试使用自动补全输入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

在PyCharm中选择/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python作为我的解释器时,显示有几个包是过时的。这些过时的包和我运行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时,也得到了同样的错误。

根据这些信息,我(希望是正确的)得出结论,我正在尝试更新苹果提供的Python,可能使用MacPorts会更适合解决这个问题。我安装了MacPorts并运行了port upgrade outdated,结果是Nothing to upgrade

我该如何将版本从2.7.5更新到2.7.6,并更新相关的包呢?

1 个回答

2

问题是,Mavericks系统似乎让构建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

撰写回答