Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
837 views
in Technique[技术] by (71.8m points)

macos - Install Python 2.7.1 side-by-side with Apple-supplied Python

I have just downloaded the Python 2.7.1 DMG from Python.org. I have seen suggestions to get ActivePython, but I would prefer the one from Python.org.

  1. If I just run the mpkg installer inside the DMG, accepting all defaults, will it live peacefully with the Apple-supplied Python?
  2. If I type python in Terminal, which one will I get?
  3. In Terminal, how do I specify to run the Apple-supplied Python? What about the Python I installed myself?
  4. What are these talk about setting the PATH when installing a different Python version? I understand that the Python installer will just set it up automatically. But I still want to peek under the hood. I know how to do this in Windows (Environment Variables). For Mac OS X, how do I tinker with the PATH?

I might as well try these out myself first, but I'm new to the Mac. Python is quite a complicated installation, writing files to different folders and configuring OS settings like PATH. TrashMe or AppCleaner might not be very effective with uninstalling Python if ever I want to go back to a clean slate. Therefore I want to gain clear insights to my questions above.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you did not change the default set of packages when using the python.org installer, typing python from a command line should run the newly-installed Python 2.7. (You will need to start a new terminal session after running the installer to see this.) The current python.org installers for OS X create a folder in your Applications directory named Python m.n depending on the Python version. If you look in /Applications/Python 2.7, you'll see a file called Update Shell Profile.command. It's a shell script; you can inspect it in an editor or with Quicklook. Its purpose is to modify the startup files for the most common shell programs on OS X (bash, sh, csh) to ensure that the directory where the new Python's executable commands are located gets added to the front of the list of directories in the PATH environment variable, so that the python commands in it will be found before the Apple-suppled python commands are found. By default, the installer runs the Update Shell Profile.command for you automatically. This should result in something like this:

$ cat ~/.bash_profile
# .bash_profile
# ... other stuff

# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

If you take a look in that directory, you should see something like this:

$ ls -l /Library/Frameworks/Python.framework/Versions/2.7/bin
total 272
lrwxr-xr-x  1 root  admin      8 Nov 30 00:49 2to3@ -> 2to3-2.7
-rwxrwxr-x  1 root  admin    140 Nov 30 00:30 2to3-2.7*
lrwxr-xr-x  1 root  admin      7 Nov 30 00:49 idle@ -> idle2.7
-rwxrwxr-x  1 root  admin    138 Nov 30 00:30 idle2.7*
lrwxr-xr-x  1 root  admin      8 Nov 30 00:49 pydoc@ -> pydoc2.7
-rwxrwxr-x  1 root  admin    123 Nov 30 00:30 pydoc2.7*
lrwxr-xr-x  1 root  admin      9 Nov 30 00:49 python@ -> python2.7
lrwxr-xr-x  1 root  admin     16 Nov 30 00:49 python-config@ -> python2.7-config
-rwxrwxr-x  1 root  admin  33764 Nov 30 00:31 python2.7*
-rwxrwxr-x  1 root  admin   1663 Nov 30 00:31 python2.7-config*
lrwxr-xr-x  1 root  admin     10 Nov 30 00:49 pythonw@ -> pythonw2.7
-rwxrwxr-x  1 root  admin  33764 Nov 30 00:31 pythonw2.7*
lrwxr-xr-x  1 root  admin     11 Nov 30 00:49 smtpd.py@ -> smtpd2.7.py
-rwxrwxr-x  1 root  admin  18586 Nov 30 00:30 smtpd2.7.py*

The new python is available as the command python2.7 but there is also a symbolic link to it as python. Because the PATH environment has been changed:

$ echo $PATH
/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

when you type python as a command in the shell, that symlink will be found first before the Apple-supplied python in /usr/bin one of the directories where system-supplied user commands are installed (as on OS X 10.6):

$ ls /usr/bin/py*
/usr/bin/pydoc*            /usr/bin/python-config*    /usr/bin/python2.6-config@
/usr/bin/pydoc2.5@         /usr/bin/python2.5@        /usr/bin/pythonw*
/usr/bin/pydoc2.6@         /usr/bin/python2.5-config@ /usr/bin/pythonw2.5@
/usr/bin/python*           /usr/bin/python2.6@        /usr/bin/pythonw2.6@

(Note, in general, you should not attempt to modify or delete files in /usr/bin since they are part of OS X and managed by Apple.)

There are many ways to manage multiple Python installations on OS X; check the archives or the web. One thing to keep in mind is that you can always use an absolute path to the desired python command to check. So with the modified path as above you should see the following behaviors:

$ /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 -c 'import sys;print(sys.version)'
2.7.1 (r271:86882M, Nov 30 2010, 09:39:13) 
[GCC 4.0.1 (Apple Inc. build 5494)]
$ python2.7 -c 'import sys;print(sys.version)'
2.7.1 (r271:86882M, Nov 30 2010, 09:39:13) 
[GCC 4.0.1 (Apple Inc. build 5494)]
$ python -c 'import sys;print(sys.version)'
2.7.1 (r271:86882M, Nov 30 2010, 09:39:13) 
[GCC 4.0.1 (Apple Inc. build 5494)]
$ /usr/bin/python -c 'import sys;print(sys.version)'
2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)]
$ /usr/bin/python2.6 -c 'import sys;print(sys.version)'
2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...