Installing / Updating Python on OS X

While Python comes pre-installed on OS X, Apple doesn’t do a good job on keeping the Python runtime environment up to date. Currently, on Mac OS X 10.7.4 “Lion”, enteringpython -VreturnsPython 2.7.1. Even worse, Mac OS X 10.6 “Snow Leopard” is still on a Python 2.6 release.
While the latest Python releases are always available on http://www.python.org, updating a Mac isn’t a simple, straight forward process.

Follow along and update your Mac to Python 2.7.3, or 3.3.0 or whatever the newest 2.x and 3.x release might be, when you read this. To update your Mac to something like Python 2.7.3, I assume that

  • your Mac-User account is setup as an “Administrator” account.
  • your Mac already has this folder: /System/Library/Frameworks/Python.framework/Versions/

To read about how to upgrade to Python 3.3, jump to the very bottom of this post.

1. Downloading and Installing the latest Python Release

Go to python.org and pick the most recent Python 2.x release: http://python.org/download.
I picked Python 2.7.3 Mac OS X 64-bit/32-bit x86-64/i386 Installer (for Mac OS X 10.6 and later), an about 18 MB download.
Opening the DMG and executing the installer (Python.mpkg) will install the Python release into /Library/Frameworks/Python.framework, which is not next to the other, already installed Python versions and may lead to some nasty incompatibilities. Moreover,/usr/bin/pythonstill executes python 2.7.1., i.e. some work still needs to be done to really update Python on your Mac.

2. Moving Python into the right place

If Python 2.7.x is already available on the Mac (e.g. on OS X Lion), open a terminal and enter:

1
sudorm-R/System/Library/Frameworks/Python.framework/Versions/2.7

This will delete Python 2.7.
In any case, now enter this into the terminal

1
sudomv/Library/Frameworks/Python.framework/Versions/2.7/System/Library/Frameworks/Python.framework/Versions

which moves the just installed python 2.7.3 release next to the other Python releases.

3. Fixing the Group

Setting group to wheel, just like it’s done for the already installed Python Versions:

1
sudochown-R root:wheel/System/Library/Frameworks/Python.framework/Versions/2.7

4. Updating the Current Link

1
2
sudorm/System/Library/Frameworks/Python.framework/Versions/Current
sudoln-s/System/Library/Frameworks/Python.framework/Versions/2.7/System/Library/Frameworks/Python.framework/Versions/Current

5. Fixing /usr/bin

Since python, pythonw, etc. are not linked but actually reside in the/usr/bindirectory,
/usr/bin/pythonstill executes python 2.7.1..
Therefore, we are going to remove the old python executable files and replacing them with links into /System/Library/Frameworks/Python.framework/Versions/Current:

5.1. Removing old copies
1
2
3
4
sudorm/usr/bin/pydoc
sudorm/usr/bin/python
sudorm/usr/bin/pythonw
sudorm/usr/bin/python-config
5.2. Creating links into /System/…/Python.framework/Versions/Current
1
2
3
4
sudoln-s/System/Library/Frameworks/Python.framework/Versions/2.7/bin/pydoc/usr/bin/pydoc
sudoln-s/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python/usr/bin/python
sudoln-s/System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw/usr/bin/pythonw
sudoln-s/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python-config/usr/bin/python-config

6. Updating .bash_profile

Use TextMate, pico, or your favorite text editor to edit the hidden ~/.bash_profile file. If you want to be able to execute python tools like idle easily, without providing the path, edit the PATH for Python like this:

1
2
3
4
# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/System/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
exportPATH

However, you can also remove those four lines altogether or fall-back to the .bash_profile.pysave file.
Try it by closing the terminal app, re-opening it, and enteringpython -Vas well as/usr/bin/python -V.

7. Updating your IDE

The last remaining step it to tell your IDE about the new Python release that you just installed.
I hope you have discovered PyCharm, JetBrain’s IDE for Python and Django.
In PyCharm, you would open its properties and under “Project Interpreters” change the Python Interpreter, by pointing it to the/System/Library/Frameworks/Python.framework/Versions/2.7directory.

Updating to Python 3.3

Following the same procedure, you can also update to Python 3.3. E.g, I downloaded the Python 3.3.0 Mac OS X 64-bit/32-bit x86-64/i386 Installer form here: http://python.org/download/

After running the included installer, a script like this (run with sudo) should put everything into the right place and make python 3.3 the default python version.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash
rm-R/System/Library/Frameworks/Python.framework/Versions/3.3
mv/Library/Frameworks/Python.framework/Versions/3.3/System/Library/Frameworks/Python.framework/Versions
chown-R root:wheel/System/Library/Frameworks/Python.framework/Versions/3.3
 
rm/System/Library/Frameworks/Python.framework/Versions/Current
ln-s/System/Library/Frameworks/Python.framework/Versions/3.3/System/Library/Frameworks/Python.framework/Versions/Current
 
rm/usr/bin/pydoc
rm/usr/bin/python
rm/usr/bin/pythonw
rm/usr/bin/python-config
 
rm/System/Library/Frameworks/Python.framework/Versions/3.3/bin/pydoc
rm/System/Library/Frameworks/Python.framework/Versions/3.3/bin/python
rm/System/Library/Frameworks/Python.framework/Versions/3.3/bin/pythonw
rm/System/Library/Frameworks/Python.framework/Versions/3.3/bin/python-config
 
ln-s/System/Library/Frameworks/Python.framework/Versions/3.3/bin/pydoc3/usr/bin/pydoc
ln-s/System/Library/Frameworks/Python.framework/Versions/3.3/bin/python3/usr/bin/python
ln-s/System/Library/Frameworks/Python.framework/Versions/3.3/bin/pythonw3/usr/bin/pythonw
ln-s/System/Library/Frameworks/Python.framework/Versions/3.3/bin/python3-

转载于:https://my.oschina.net/yangphere/blog/141344

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的错误信息,看起来是权限问题导致安装 `pbr` 失败。具体来说,错误提示了 `/usr/lib/python2.7/site-packages/pbr` 目录的权限被拒绝。 要解决这个问题,你可以尝试以下几种方法: 1. 使用管理员权限重新运行安装命令。在命令行中加上 `sudo` 命令来提升权限,例如: ``` sudo pip install pbr ``` 这会使用管理员权限来运行安装命令,解决权限被拒绝的问题。 2. 如果你没有管理员权限,可以尝试使用 `--user` 参数来进行用户级别的安装。这样,`pbr` 将被安装在用户主目录下的 `.local` 目录中,而不是系统目录。使用以下命令进行安装: ``` pip install --user pbr ``` 这会将 `pbr` 安装到你的用户目录下,避免权限问题。 3. 如果上述方法都无效,你可以尝试创建并设置正确的文件夹权限。首先,使用以下命令创建一个目录: ``` sudo mkdir -p /usr/lib/python2.7/site-packages/pbr ``` 接下来,使用以下命令更改目录的所有者和权限: ``` sudo chown -R your_username:your_username /usr/lib/python2.7/site-packages/pbr ``` 将 `your_username` 替换为你的用户名。这将确保你有足够的权限来安装 `pbr`。 4. 此外,根据错误信息中的提示,你可以考虑升级 `pip` 到最新版本。使用以下命令升级 `pip`: ``` pip install --upgrade pip ``` 然后再尝试安装 `pbr`。 希望这些方法能够帮助你解决问题。如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值