将 Python 2.7.1 与 Apple 提供的 Python 并行安装
我刚刚从 Python.org 下载了 Python 2.7.1 DMG。我看到过有关获取 ActivePython 的建议,但我更喜欢 Python.org 中的那个。
- 如果我只是在 DMG 中运行 mpkg 安装程序并接受所有默认设置,它是否可以与 Apple 提供的 Python 和平共处?
- 如果我在终端中输入
python
,我会得到哪一个? - 在终端中,如何指定运行 Apple 提供的 Python?那我自己安装的Python呢?
- 这些关于安装不同 Python 版本时设置
PATH
的讨论是什么?据我所知,Python 安装程序会自动设置它。但我仍然想看看幕后花絮。我知道如何在 Windows(环境变量)中执行此操作。对于 Mac OS X,如何修改PATH
?
我不妨先自己尝试一下,但我是 Mac 新手。 Python 的安装相当复杂,将文件写入不同的文件夹并配置操作系统设置,例如 PATH
。如果我想回到干净的状态,TrashMe 或 AppCleaner 可能对卸载 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.
- If I just run the
mpkg
installer inside the DMG, accepting all defaults, will it live peacefully with the Apple-supplied Python? - If I type
python
in Terminal, which one will I get? - In Terminal, how do I specify to run the Apple-supplied Python? What about the Python I installed myself?
- 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 thePATH
?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您在使用 python.org 安装程序时没有更改默认的软件包集,则从命令行输入
python
应运行新安装的 Python 2.7。 (运行安装程序后,您需要启动一个新的终端会话才能看到这一点。)当前 OS X 的 python.org 安装程序根据 Python 版本在您的应用程序目录中创建一个名为Python mn
的文件夹。如果您查看/Applications/Python 2.7
,您将看到一个名为Update Shell Profile.command
的文件。这是一个 shell 脚本;您可以在编辑器中或使用 Quicklook 来检查它。其目的是修改 OS X 上最常见的 shell 程序(bash
、sh
、csh
)的启动文件,以确保目录新的 Python 可执行命令所在的位置被添加到 PATH 环境变量中目录列表的前面,以便在找到 Apple 提供的 python 命令之前找到其中的 python 命令。默认情况下,安装程序会自动为您运行Update Shell Profile.command
。这应该会导致如下结果:如果您查看该目录,您应该会看到如下内容:
新的 python 可作为命令
python2.7
使用,但还有一个符号链接它是python
。因为 PATH 环境已更改:当您在 shell 中输入
python
作为命令时,将首先在/ 中的 Apple 提供的
安装系统提供的用户命令的目录之一(如 OS X 10.6):(python
之前找到该符号链接。 usr/bin注意,一般情况下,您不应尝试修改或删除
/usr/bin 中的文件
因为它们是 OS X 的一部分并由 Apple 管理。)有很多方法可以在 OS X 上管理多个 Python 安装;检查档案或网络。需要记住的一件事是,您始终可以使用所需 python 命令的绝对路径进行检查。因此,使用上面修改后的路径,您应该会看到以下行为:
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 namedPython m.n
depending on the Python version. If you look in/Applications/Python 2.7
, you'll see a file calledUpdate 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 theUpdate Shell Profile.command
for you automatically. This should result in something like this:If you take a look in that directory, you should see something like this:
The new python is available as the command
python2.7
but there is also a symbolic link to it aspython
. Because the PATH environment has been changed:when you type
python
as a command in the shell, that symlink will be found first before the Apple-suppliedpython
in/usr/bin
one of the directories where system-supplied user commands are installed (as on OS X 10.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:
which python
进行检查。python_mac
和python_standard
)。which python
.python_mac
andpython_standard
).如果您使用virtualenv,则可以使不同的Python安装特别无阻碍地运行。
您可以按照以下有关
将指导您在不同的地方安装不同版本的 python,每个版本都有自己的一组站点包,并让它们分别在不同的项目上工作。强烈建议您查看它,因为它还解释了如何以简单的方式更改 PATH 变量,以便您的 shell 命令可以找到不同的 python 安装。
这里有一些关于不同 python 安装位置的快速信息。
在终端输入
which python
会告诉你它的位置。您还可以使用 python -V 查看使用的版本。请记住,python
和python2.x
可以是不同的东西。您可以为不同的 python 安装分配适当的名称,以便使用alias
命令在终端或脚本中调用它们。例如,Apple 提供的系统 python 安装在
/System/Library/Frameworks/Python.framework
和/usr/bin/python
要检查当前系统Python是什么,您可以使用
python -c '导入 sys;print(sys.version)'
您下载并运行的打包安装程序通常会安装到
/Library/Frameworks/Python.framework/Versions/2.7/bin/
MacPorts 会将 python 或 python26 和 python27 等安装到
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/
您可以使用常用的
sudo port uninstall packageName
来卸载它们,如果您想删除依赖项,请使用类似以下代码的内容sudo port uninstall python27 --follow-dependents安装
您尝试使用
port
安装的所有其他内容(例如 numpy、scipy、PIL、opencv 等)都将转到/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
Different python installations can be made to run without hinderance specially, if you use
virtualenv
You can follow these article about
These will instruct you on installing different versions of python in different places each with their own set of site-packages and have them individually work on different projects. Highly recommend you look into it as it also explains how to change the
PATH
variable in an easy way so that different python installations can be found by your shell commands.Here is some quick info about where different python installations go to..
Typing
which python
at the terminal will tell you its location. You can also usepython -V
to see the version used. Remember,python
andpython2.x
can be different things. You can assign appropriate names to different python installations in order to call them at the terminal or in your scripts by usingalias
command. For instance,Apple supplied system python is installed at
/System/Library/Frameworks/Python.framework
and/usr/bin/python
To check what is currently the system python you can use
python -c 'import sys;print(sys.version)'
Packaged installers that you download and run typically install to
/Library/Frameworks/Python.framework/Versions/2.7/bin/
MacPorts will install python or something like python26 and python27 to
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/
You can uninstall them by using usual
sudo port uninstall packageName
and if you want to remove dependencies use something like this codesudo port uninstall python27 --follow-dependents installed
All additional things like numpy, scipy, PIL, opencv, etc. that you try to install using
port
will go to/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages