开发者

Trying to upgrade Python to 3.0 on Mac OS 10.5.8

I'm having some problems upgrading Python on my Mac. For my first attempt, I downloaded and installed the 2.6.4 dmg MacPython installer from http://python.org/download/mac/. This did install 2.6.4, and when I ran 'python' from the terminal it says that version.

However, I also had a test script where I am doing:

import os, json

But I get an error that the 'json' library was not found. In the script I included this shebang at the top to make it run from the terminal:

#! /usr/bin/python

I suspect that the symlinks that come directly from Apple that point to Python 2.5 were not updated by the 2.6.4 installer, so directly from the terminal 'python' is running the newer version, but my test.py file is executing 2.5.

So at this point I read a couple of other SO pages on doing this upgrade, and people recommended using 3rd party packages that sit side-by-side so as not to break the OS-level dependencies on v2.5. I then found ActivePython offered a 3.x installer(that was also recommended on another SO page). I installed that, but 'python' still shows 2.6.4 and my script still can't find the json library.

Finally, I'm baffled at how to safely remove MacPython( the Mac installer I mentioned above ). There's one sentence on the page that says to remove some things that seem pretty vital to Python on the Mac. Quote:

  • A MacPython 2.5 folder in your Applications folder. In here you find IDLE, the development environment that is a standard part of official Python distributions; PythonLauncher, which handles double-clicking Python scripts from the Finder; and the “Build Applet” tool, which allows you to package Python scripts as standalone applications on your system.
  • A framework /Library/Frameworks/Python.framework, which includes the Python executable and libraries. The installer adds this location to your shell path. To uninstall MacPython, you can simply remove these three things. A symlink to the Python executable is placed in /usr/local/bin/.

So now I have 开发者_如何学Python3 versions of Python installed and I'm not sure how to resolve this stupid mess.


First, /usr/bin/python should always point to the Apple-supplied python and on 10.5 that means python2.5. Don't change this!

When you installed the python.org python2.6, by default it installs symlinks in /usr/local/bin/ so one way to invoke it is /usr/local/bin/python2.6 or, most likely, just python2.6. Since json was added to the python library in python 2.6, you'll find the json module is there. One way to solve your orignal problem then is to change the shebang line to be:

#!/usr/bin/env python2.6

Also by default, the python.org installer updates your shell profile to add its bin directory to your $PATH, which is why typing python probably now invokes python2.6.

You shouldn't need to but if you really want to remove all traces of the python.org 2.6:

  1. Delete the extra lines at the end of your .bash_profile and/or .profile by reverting to .bash_profile.pysave and .profile.pysave.

  2. Remove the python2.6 framework directory:

    sudo rm -r /Library/Frameworks/Python.framework/Versions/2.6

  3. Remove IDLE and the extras installed in /Applications:

    sudo rm -r /Applications/Python\ 2.6

Also there's nothing wrong with moving on to Python 3. For the moment, both Python 2 and Python 3 are being actively developed; search the archives for the various pros and cons. However, Python 3.0 should not be used. Not surprisingly for something that major, Python 3.0 had a number of serious first-time bugs so, with the release of Python 3.1, 3.0 support was immediately dropped.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜