将 Python 2.7.1 与 Apple 提供的 Python 并行安装

发布于 2024-10-08 02:27:28 字数 670 浏览 0 评论 0原文

我刚刚从 Python.org 下载了 Python 2.7.1 DMG。我看到过有关获取 ActivePython 的建议,但我更喜欢 Python.org 中的那个。

  1. 如果我只是在 DMG 中运行 mpkg 安装程序并接受所有默认设置,它是否可以与 Apple 提供的 Python 和平共处?
  2. 如果我在终端中输入 python,我会得到哪一个?
  3. 在终端中,如何指定运行 Apple 提供的 Python?那我自己安装的Python呢?
  4. 这些关于安装不同 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.

  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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人练习一个人 2024-10-15 02:27:28

如果您在使用 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 程序(bashshcsh)的启动文件,以确保目录新的 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

如果您查看该目录,您应该会看到如下内容:

$ 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 环境已更改:

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

当您在 shell 中输入 python 作为命令时,将首先在 / 中的 Apple 提供的 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@

注意,一般情况下,您不应尝试修改或删除 /usr/bin 中的文件 因为它们是 OS X 的一部分并由 Apple 管理。)

有很多方法可以在 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)]

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)]
韶华倾负 2024-10-15 02:27:28
  1. 是的,它会工作得很好。
  2. 您必须更改适当的符号链接。您始终可以使用 which python 进行检查。
  3. 您可以为不同版本创建额外的符号链接(例如python_macpython_standard)。
  4. 阅读本文
  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_mac and python_standard).
  4. Read this.
夏末染殇 2024-10-15 02:27:28

如果您使用virtualenv,则可以使不同的Python安装特别无阻碍地运行。

您可以按照以下有关

  1. 引导虚拟环境
  2. 设置 python这些

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

这里有一些关于不同 python 安装位置的快速信息。

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

alias /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 sys-py27
  • Apple 提供的系统 python 安装在 /System/Library/Frameworks/Python.framework/usr/bin/python

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

      python -c '导入 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安装

    • 您尝试使用 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

  1. bootstrapping virtual environments
  2. setting up python ecosystems

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 use python -V to see the version used. Remember, python and python2.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 using alias command. For instance,

alias /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 sys-py27
  • Apple supplied system python is installed at /System/Library/Frameworks/Python.framework and /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 packageName and 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 port will go to

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文