与Apple提供的Python并排安装Python 2.7.1 [英] Install Python 2.7.1 side-by-side with Apple-supplied Python

查看:99
本文介绍了与Apple提供的Python并排安装Python 2.7.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚从 Python.org .我已经看到了获取ActivePython的建议,但我更希望使用Python.org的建议.

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. 如果我只是在DMG中运行mpkg安装程序,并接受所有默认设置,它是否可以与Apple提供的Python和平共处?
  2. 如果我在Terminal中键入python,我会得到哪一个?
  3. 在终端中,如何指定运行Apple提供的Python?我自己安装的Python呢?
  4. 在安装其他Python版本时,关于设置PATH的这些讨论是什么?我知道Python安装程序会自动设置它.但是我仍然想在幕后偷看.我知道如何在Windows(环境变量)中执行此操作.对于Mac OS X,如何修改PATH?
  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?

我不妨先自己尝试一下这些方法,但是我是Mac的新手. Python是非常复杂的安装,将文件写入不同的文件夹并配置操作系统设置,例如PATH.如果我想回到干净的状态,TrashMe或AppCleaner对于卸载Python可能不是很有效.因此,我想对我的上述问题获得清晰的见识.

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.

推荐答案

如果在使用python.org安装程序时未更改默认软件包集,则在命令行中输入python应该会运行新安装的Python 2.7. (运行安装程序后,您需要启动一个新的终端会话才能看到此内容.)当前OS X的python.org安装程序在Python的Applications目录中创建一个名为Python m.n的文件夹,具体取决于Python版本.如果您查看/Applications/Python 2.7,将会看到一个名为Update Shell Profile.command的文件.这是一个shell脚本;您可以在编辑器中或使用Quicklook对其进行检查.其目的是为OS X上最常见的shell程序(bashshcsh)修改启动文件,以确保将新Python可执行命令所在的目录添加到Windows XP的开头. PATH环境变量中的目录列表,以便在找到Apple提供的python命令之前先找到其中的python命令.默认情况下,安装程序会自动为您运行Update Shell Profile.command.这应该导致如下结果:

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*

新的python可作为命令python2.7使用,但也有一个符号链接作为python.由于PATH环境已更改:

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

当您在外壳程序中键入python作为命令时,该符号链接将首先在/usr/bin中的Apple提供的python之前找到,该目录位于安装系统提供的用户命令的目录之一中(如在OS上) X 10.6):

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@

(请注意,通常,您不应尝试在/usr/bin中修改或删除文件,因为它们是OS X的一部分,并且由Apple管理.)

(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.)

有许多方法可以在OS X上管理多个Python安装.检查档案或网络.要记住的一件事是,您始终可以使用所需python命令的绝对路径进行检查.因此,使用上述修改后的路径,您应该会看到以下行为:

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)]

这篇关于与Apple提供的Python并排安装Python 2.7.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆