macos 与 Apple 提供的 Python 并排安装 Python 2.7.1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4460367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:27:08  来源:igfitidea点击:

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

pythonmacososx-snow-leopard

提问by Kit

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.

我刚刚从Python.org下载了 Python 2.7.1 DMG 。我已经看到了获取 ActivePython 的建议,但我更喜欢来自 Python.org 的建议。

  1. If I just run the mpkginstaller inside the DMG, accepting all defaults, will it live peacefully with the Apple-supplied Python?
  2. If I type pythonin 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 PATHwhen 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?
  1. 如果我只是mpkg在 DMG 中运行安装程序,接受所有默认设置,它会与 Apple 提供的 Python 和平相处吗?
  2. 如果我输入python终端,我会得到哪一个?
  3. 在终端中,如何指定运行 Apple 提供的 Python?我自己安装的 Python 呢?
  4. PATH在安装不同的 Python 版本时,这些是什么设置?我知道 Python 安装程序会自动设置它。但我仍然想偷看引擎盖下。我知道如何在 Windows(环境变量)中执行此操作。对于 Mac OS X,我如何修改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.

我不妨先自己尝试一下,但我是 Mac 的新手。Python 是一个相当复杂的安装,将文件写入不同的文件夹并配置操作系统设置,如PATH. 如果我想回到一个干净的状态,TrashMe 或 AppCleaner 在卸载 Python 时可能不是很有效。因此,我想清楚地了解我的上述问题。

回答by Ned Deily

If you did not change the default set of packages when using the python.org installer, typing pythonfrom 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.ndepending 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.commandfor you automatically. This should result in something like this:

如果您在使用 python.org 安装程序时没有更改默认包集,则从命令行键入python应该运行新安装的 Python 2.7。(您需要在运行安装程序后启动一个新的终端会话才能看到这一点。) OS X 的当前 python.org 安装程序在您的应用程序目录中创建一个文件夹,该文件夹Python m.n根据 Python 版本命名。如果您查看/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会自动为您运行。这应该导致这样的事情:

$ 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.7but there is also a symbolic link to it as python. Because the PATH environment has been changed:

新的 python 可用作命令,python2.7但也有一个符号链接作为python. 因为PATH环境变了:

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

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

当键入python作为壳的命令时,该符号链接将苹果提供的之前首先被发现python/usr/bin其中系统提供的用户命令的安装(如在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/binsince they are part of OS X and managed by Apple.)

(请注意,一般情况下,您不应尝试修改或删除文件,/usr/bin因为它们是 OS X 的一部分并由 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:

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

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

回答by Bj?rn Pollex

  1. Yes, it will work fine.
  2. You will have to change the appropriate symlink. You can always check using which python.
  3. You can create extra symlinks for the different versions (e.g. python_macand python_standard).
  4. Read this.
  1. 是的,它会正常工作。
  2. 您将不得不更改适当的符号链接。您始终可以使用which python.
  3. 您可以为不同的版本(例如python_macpython_standard)创建额外的符号链接。
  4. 这个

回答by samkhan13

Different python installations can be made to run without hinderance specially, if you use virtualenv

不同的python安装可以特别无阻碍地运行,如果你使用 virtualenv

You can follow these article about

你可以关注这些文章

  1. bootstrapping virtual environments
  2. setting up python ecosystems
  1. 引导虚拟环境
  2. 建立python生态系统

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 PATHvariable in an easy way so that different python installations can be found by your shell commands.

这些将指导您在不同的地方安装不同版本的 python,每个地方都有自己的一组站点包,并让它们分别用于不同的项目。强烈建议您查看它,因为它还解释了如何以PATH简单的方式更改变量,以便您的 shell 命令可以找到不同的 python 安装。

Here is some quick info about where different python installations go to..

这里有一些关于不同 python 安装去哪里的快速信息..

Typing which pythonat the terminal will tell you its location. You can also use python -Vto see the version used. Remember, pythonand python2.xcan 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 using aliascommand. For instance,

which python在终端打字会告诉你它的位置。您还可以使用python -V查看所使用的版本。请记住,python并且python2.x可以是不同的东西。您可以为不同的 python 安装分配适当的名称,以便使用alias命令在终端或脚本中调用它们。例如,

alias /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 sys-py27
  • Apple supplied system python is installed at /System/Library/Frameworks/Python.frameworkand /usr/bin/python

    • Indeed, you shouldn't mess with these by trying to delete them manually. The system python could be in use by other software in your computer.
    • 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/

    • You can manually remove the Python.framework folder. You might also see Python in the Applications folder along with other things like IDLE, AppBuilder and PythonLauncher. You can just go ahead and delete those if you need to.
  • 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 packageNameand if you want to remove dependencies use something like this code

      sudo port uninstall python27 --follow-dependents installed

    • All additional things like numpy, scipy, PIL, opencv, etc. that you try to install using portwill go to

      /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

  • Apple 提供的系统 python 安装在/System/Library/Frameworks/Python.framework/usr/bin/python

    • 实际上,您不应该尝试手动删除它们来弄乱它们。系统 python 可能被您计算机中的其他软件使用。
    • 要检查您可以使用的当前系统 python

      python -c 'import sys;print(sys.version)'

  • 您下载并运行的打包安装程序通常安装到 /Library/Frameworks/Python.framework/Versions/2.7/bin/

    • 您可以手动删除 Python.framework 文件夹。您可能还会在 Applications 文件夹中看到 Python 以及 IDLE、AppBuilder 和 PythonLauncher 等其他内容。如果需要,您可以继续删除它们。
  • MacPorts 将安装 python 或 python26 和 python27 之类的东西 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/

    • 您可以使用通常的方法卸载它们sudo port uninstall packageName,如果要删除依赖项,请使用类似此代码的内容

      sudo port uninstall python27 --follow-dependents installed

    • 您尝试安装的所有其他内容,如 numpy、scipy、PIL、opencv 等都port将转到

      /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages