在OS X上安装python 2的最佳方法是什么?

时间:2022-05-27 23:14:18

A colleague of mine wants to use my python 2 code on his OS X (10.6) machine. My code imports several built-in python packages, including Tkinter and shelve, and also uses third-party packages, including numpy, scipy, matplotlib, and ipython.

我的一位同事希望在他的OS X(10.6)机器上使用我的python 2代码。我的代码导入了几个内置的python包,包括Tkinter和shelve,还使用了第三方包,包括numpy,scipy,matplotlib和ipython。

I've encountered a few problems with OS X's built-in python. (IDLE doesn't work, for example*). I suspect I should install a more recent version of python, and a different version of Tk.

我在OS X的内置python中遇到了一些问题。 (IDLE不起作用,例如*)。我怀疑我应该安装更新版本的python,以及不同版本的Tk。

My questions:

我的问题:

  1. Will having two different versions of python/Tk on the same machine cause problems?

    在同一台机器上有两个不同版本的python / Tk会导致问题吗?

  2. I would like to associate the terminal commands 'python', 'ipython', and 'easy_install' with the more recent version of python. How should I do this?

    我想将终端命令'python','ipython'和'easy_install'与更新版本的python相关联。我该怎么做?

  3. When I install third-party packages like numpy using a .dmg file, how do I control which version of python numpy installs into?

    当我使用.dmg文件安装numpy等第三方软件包时,如何控制安装哪个版本的python numpy?

  4. Is there a better way to do this?

    有一个更好的方法吗?

If this process goes well, I'd consider adding OS X instructions to my code's documentation, so I'd like to boil down this process to the simplest, most general approach.

如果这个过程进展顺利,我会考虑将OS X指令添加到我的代码文档中,所以我想把这个过程归结为最简单,最通用的方法。

*EDIT: Also, this

*编辑:此外,这

EDIT: Thank you everyone for the useful answers. My colleague tried MacPorts, which seems to work well, but has a few speedbumps. First we had to install Xcode from the system install disk. This is not a fast or lightweight install (several GB). Luckily we still had the disk! Once Xcode was installed, MacPorts was easy to install. Python and the python subpackages we needed were also easy to install, but he told me this installation took several hours. Presumably this delay is due to compilation? He had an easy time setting the MacPorts python as default. However, I think we have to change the 'Python Launcher' application by hand, this seems to still default to the system python.

编辑:谢谢大家的有用答案。我的同事尝试了MacPorts,它似乎运行良好,但有几个speedbumps。首先,我们必须从系统安装盘安装Xcode。这不是快速或轻量级安装(几GB)。幸运的是我们还有磁盘!安装Xcode后,MacPorts易于安装。 Python和我们需要的python子包也很容易安装,但他告诉我这个安装需要几个小时。据推测,这种延迟是由于汇编造成的?他很容易将MacPorts python设置为默认值。但是,我认为我们必须手动更改'Python Launcher'应用程序,这似乎仍然默认为系统python。

Even though he has a working system now, I'm tempted to ask him to try one of the other solutions. I'm not sure all of my code's potential users will tolerate a multi-hour, multi-gigabyte installation.

即使他现在有一个工作系统,我很想让他尝试其他解决方案之一。我不确定我的所有代码的潜在用户是否会容忍多小时,多GB的安装。

6 个解决方案

#1


2  

Using MacPorts, you can install python 2.6, 2.7, 3.1 and 3.2 at the same time, with their own packages, without ever touching the built-in python.

使用MacPorts,您可以使用自己的软件包同时安装python 2.6,2.7,3.1和3.2,而无需触及内置的python。

numpy, scipy, matplotlib, and ipython are also available as ports for most of those python versions.

numpy,scipy,matplotlib和ipython也可用作大多数python版本的端口。

Moreover, if you install the python_select port, you'll be able:

此外,如果您安装python_select端口,您将能够:

  • to choose which one of those (plus the built-in python) is the "default" python;

    选择其中哪一个(加上内置的python)是“默认”python;

  • to install python packages through easy_install/pip for the "selected" python, if they're not available as ports.

    如果它们不能用作端口,则通过easy_install / pip为“选定”python安装python包。

Add virtualenv to the mix, and you'll have a very, very flexible Python development environment.

将virtualenv添加到组合中,您将拥有一个非常非常灵活的Python开发环境。

As for your questions:

至于你的问题:

Q1: with MacPorts, no. while not a frequent user, I've installed and used matplotlib in 2.6 and 2.7, switching between the two using python_select.

Q1:使用MacPorts,没有。虽然不是常用的用户,但我已经在2.6和2.7中安装并使用了matplotlib,使用python_select在两者之间切换。

Q2: easy_install, pip, ipython will be "linked" to the python they were installed by. (but see tip 1)

Q2:easy_install,pip,ipython将“链接”到他们安装的python上。 (但见提示1)

Q3: it's easier to install one of the py{26,27,xx}-numpy ports, or pip install numpy under your python_select'ed python.

问题3:在python_select'ed python下安装py {26,27,xx} -numpy端口或pip install numpy更容易。

Q4: well, MacPorts is the best thing I know after APT on Debian/Ubuntu... :-)

Q4:嗯,MacPorts是我在Debian / Ubuntu APT之后知道的最好的东西... :-)

Now, two tips if you try MacPorts:

现在,如果你尝试MacPorts,有两个提示:

  1. MacPorts cleanly installs ports separately from the OS X installation, in an /opt/local directory, and each python version is installed in a /opt/local/Library/Frameworks/Python.framework/Versions/{2.5,2.6,2.7,...} directory. Using python_select cleanly switch the "python" command using links. BUT... the Versions/{2.5,2.6,2.7,...}/bin directory, where python scripts are installed, is not added to the PATH. Just adding: export PATH=/opt/local/Library/Frameworks/Python.framework/Versions/Current/bin:$PATH to your ~/.profile will always give you direct access to the scripts installed for the selected python.

    MacPorts在OS X安装中与/ opt / local目录中分别安装端口,每个python版本都安装在/opt/local/Library/Frameworks/Python.framework/Versions/ {2.5,2.6,2.7中。 ..} 目录。使用python_select干净地使用链接切换“python”命令。但是......版本/ {2.5,2.6,2.7,...} / bin目录(其中安装了python脚本)未添加到PATH中。只需添加:export PATH = / opt / local / Library / Frameworks / Python.framework / Versions / Current / bin:$ PATH到〜/ .profile将始终让您直接访问为所选python安装的脚本。

  2. to avoid bad surprises, I've added a echo Selected python is \"$(python_select -s)\" line to my ~/.profile, so I always know which is my currently selected python when opening a session... :-)

    为了避免不好的惊喜,我添加了一个echo选择的python是\“$(python_select -s)\”行到我的〜/ .profile,所以我总是知道在打开一个会话时哪个是我当前选择的python ...: - )

Regards,

问候,

Georges

乔治

#2


9  

I use brew to install all my libraries/compilers/interpreters.

我使用brew来安装我的所有库/编译器/解释器。

To install python try this:

要安装python试试这个:

brew install python

Then add Python's binaries directory to your $PATH in your ~/.profile:

然后将Python的二进制文件目录添加到〜/ .profile中的$ PATH:

export PATH=`brew --prefix python`/bin:$PATH

I'd recommend you to install pip, virtualenv and virtualenvwrapper to have better control over your environment too.

我建议你安装pip,virtualenv和virtualenvwrapper来更好地控制你的环境。

#3


5  

Have you tried ActivePython?

你试过ActivePython吗?

It includes a package manager (PyPM) that, by default, installs into your home directory (eg: ~/Library/Python/2.7). Main scripts get symlinked in /usr/local/bin; use the included pythonselect to set the active Python version.

它包括一个包管理器(PyPM),默认情况下,它安装在您的主目录中(例如:〜/ Library / Python / 2.7)。主脚本在/ usr / local / bin中进行符号链接;使用包含的pythonselect设置活动的Python版本。

You don't have to bother installing .dmg packages, as PyPM is a binary package manager ... therefore you can install non-pure Python packages like NumPy without having to compile things yourself.

您不必费心安装.dmg软件包,因为PyPM是一个二进制软件包管理器...因此您可以安装非纯Python软件包,如NumPy,而无需自己编译。

ActivePython can use Apple's Tcl/Tk or, if installed, ActiveTcl.

ActivePython可以使用Apple的Tcl / Tk,或者如果安装了ActiveTcl,则可以使用。

A "simplest, most general approach" in your documentation could be:

您的文档中的“最简单,最通用的方法”可能是:

  1. Install ActivePython 2.7
  2. 安装ActivePython 2.7
  3. Open Terminal and type pypm-2.7 install matplotlib ipython
  4. 打开终端并键入pypm-2.7安装matplotlib ipython

#4


2  

In almost all cases, the best python to use is the one from http://python.org/. It sets up the paths correctly and doesn't overwrite anything. DMG package installs usually work automatically, as does python setup.py install, and it's not too hard to get setuptools to work. If you want per-user installs, it is easy to set up .pydistutils.cfg and python automatically recognizes the path install_lib = ~/Library/Python/$py_version_short/site-packages

在几乎所有情况下,最好使用的python是来自http://python.org/的python。它正确设置路径,不会覆盖任何内容。 DMG包安装通常会自动工作,python setup.py安装也是如此,并且让setuptools工作并不困难。如果你想要每个用户安装,很容易设置.pydistutils.cfg和python自动识别路径install_lib =〜/ Library / Python / $ py_version_short / site-packages

#5


1  

Here is an old post that answers your questions too.

这是一篇旧帖子,也回答了你的问题。

In general it is not a problem at all to have more than one python installation on your machine. You just have to watch out which one you are calling on the command line.

通常,在您的计算机上安装多个python根本不是问题。您只需要在命令行中注意要调用的是哪一个。

>> which python 

... helps to identify where your python binary is located. The original Mac OS X python is usually at "/usr/bin/python"

...有助于确定python二进制文件的位置。最初的Mac OS X python通常位于“/ usr / bin / python”

I personally use the MacPorts python installation. It also supports you with the installation of modules. (see link above)

我个人使用MacPorts python安装。它还支持您安装模块。 (见上面的链接)

#6


-1  

I have 4 versions of python on my MacBook Pro. 2 from the original install of OS X 10.6 and a subsequent update, then self installed copies of python 2.7 and 3.2. You can update the python command to point at any of the versions. They all install in separate directories and cause no problems with each other.

我的MacBook Pro上有4个版本的python。 2.从原来安装的OS X 10.6和随后的更新,然后自行安装python 2.7和3.2的副本。您可以更新python命令以指向任何版本。它们都安装在不同的目录中,彼此之间没有任何问题。

I'm not sure what will happen when you install from a .dmg file. I believe it will simply use whatever version python points to.

我不确定从.dmg文件安装时会发生什么。我相信它只会使用python指向的任何版本。

This post on superuser.com answers your questions on changing default paths.

superuser.com上的这篇文章回答了有关更改默认路径的问题。

#1


2  

Using MacPorts, you can install python 2.6, 2.7, 3.1 and 3.2 at the same time, with their own packages, without ever touching the built-in python.

使用MacPorts,您可以使用自己的软件包同时安装python 2.6,2.7,3.1和3.2,而无需触及内置的python。

numpy, scipy, matplotlib, and ipython are also available as ports for most of those python versions.

numpy,scipy,matplotlib和ipython也可用作大多数python版本的端口。

Moreover, if you install the python_select port, you'll be able:

此外,如果您安装python_select端口,您将能够:

  • to choose which one of those (plus the built-in python) is the "default" python;

    选择其中哪一个(加上内置的python)是“默认”python;

  • to install python packages through easy_install/pip for the "selected" python, if they're not available as ports.

    如果它们不能用作端口,则通过easy_install / pip为“选定”python安装python包。

Add virtualenv to the mix, and you'll have a very, very flexible Python development environment.

将virtualenv添加到组合中,您将拥有一个非常非常灵活的Python开发环境。

As for your questions:

至于你的问题:

Q1: with MacPorts, no. while not a frequent user, I've installed and used matplotlib in 2.6 and 2.7, switching between the two using python_select.

Q1:使用MacPorts,没有。虽然不是常用的用户,但我已经在2.6和2.7中安装并使用了matplotlib,使用python_select在两者之间切换。

Q2: easy_install, pip, ipython will be "linked" to the python they were installed by. (but see tip 1)

Q2:easy_install,pip,ipython将“链接”到他们安装的python上。 (但见提示1)

Q3: it's easier to install one of the py{26,27,xx}-numpy ports, or pip install numpy under your python_select'ed python.

问题3:在python_select'ed python下安装py {26,27,xx} -numpy端口或pip install numpy更容易。

Q4: well, MacPorts is the best thing I know after APT on Debian/Ubuntu... :-)

Q4:嗯,MacPorts是我在Debian / Ubuntu APT之后知道的最好的东西... :-)

Now, two tips if you try MacPorts:

现在,如果你尝试MacPorts,有两个提示:

  1. MacPorts cleanly installs ports separately from the OS X installation, in an /opt/local directory, and each python version is installed in a /opt/local/Library/Frameworks/Python.framework/Versions/{2.5,2.6,2.7,...} directory. Using python_select cleanly switch the "python" command using links. BUT... the Versions/{2.5,2.6,2.7,...}/bin directory, where python scripts are installed, is not added to the PATH. Just adding: export PATH=/opt/local/Library/Frameworks/Python.framework/Versions/Current/bin:$PATH to your ~/.profile will always give you direct access to the scripts installed for the selected python.

    MacPorts在OS X安装中与/ opt / local目录中分别安装端口,每个python版本都安装在/opt/local/Library/Frameworks/Python.framework/Versions/ {2.5,2.6,2.7中。 ..} 目录。使用python_select干净地使用链接切换“python”命令。但是......版本/ {2.5,2.6,2.7,...} / bin目录(其中安装了python脚本)未添加到PATH中。只需添加:export PATH = / opt / local / Library / Frameworks / Python.framework / Versions / Current / bin:$ PATH到〜/ .profile将始终让您直接访问为所选python安装的脚本。

  2. to avoid bad surprises, I've added a echo Selected python is \"$(python_select -s)\" line to my ~/.profile, so I always know which is my currently selected python when opening a session... :-)

    为了避免不好的惊喜,我添加了一个echo选择的python是\“$(python_select -s)\”行到我的〜/ .profile,所以我总是知道在打开一个会话时哪个是我当前选择的python ...: - )

Regards,

问候,

Georges

乔治

#2


9  

I use brew to install all my libraries/compilers/interpreters.

我使用brew来安装我的所有库/编译器/解释器。

To install python try this:

要安装python试试这个:

brew install python

Then add Python's binaries directory to your $PATH in your ~/.profile:

然后将Python的二进制文件目录添加到〜/ .profile中的$ PATH:

export PATH=`brew --prefix python`/bin:$PATH

I'd recommend you to install pip, virtualenv and virtualenvwrapper to have better control over your environment too.

我建议你安装pip,virtualenv和virtualenvwrapper来更好地控制你的环境。

#3


5  

Have you tried ActivePython?

你试过ActivePython吗?

It includes a package manager (PyPM) that, by default, installs into your home directory (eg: ~/Library/Python/2.7). Main scripts get symlinked in /usr/local/bin; use the included pythonselect to set the active Python version.

它包括一个包管理器(PyPM),默认情况下,它安装在您的主目录中(例如:〜/ Library / Python / 2.7)。主脚本在/ usr / local / bin中进行符号链接;使用包含的pythonselect设置活动的Python版本。

You don't have to bother installing .dmg packages, as PyPM is a binary package manager ... therefore you can install non-pure Python packages like NumPy without having to compile things yourself.

您不必费心安装.dmg软件包,因为PyPM是一个二进制软件包管理器...因此您可以安装非纯Python软件包,如NumPy,而无需自己编译。

ActivePython can use Apple's Tcl/Tk or, if installed, ActiveTcl.

ActivePython可以使用Apple的Tcl / Tk,或者如果安装了ActiveTcl,则可以使用。

A "simplest, most general approach" in your documentation could be:

您的文档中的“最简单,最通用的方法”可能是:

  1. Install ActivePython 2.7
  2. 安装ActivePython 2.7
  3. Open Terminal and type pypm-2.7 install matplotlib ipython
  4. 打开终端并键入pypm-2.7安装matplotlib ipython

#4


2  

In almost all cases, the best python to use is the one from http://python.org/. It sets up the paths correctly and doesn't overwrite anything. DMG package installs usually work automatically, as does python setup.py install, and it's not too hard to get setuptools to work. If you want per-user installs, it is easy to set up .pydistutils.cfg and python automatically recognizes the path install_lib = ~/Library/Python/$py_version_short/site-packages

在几乎所有情况下,最好使用的python是来自http://python.org/的python。它正确设置路径,不会覆盖任何内容。 DMG包安装通常会自动工作,python setup.py安装也是如此,并且让setuptools工作并不困难。如果你想要每个用户安装,很容易设置.pydistutils.cfg和python自动识别路径install_lib =〜/ Library / Python / $ py_version_short / site-packages

#5


1  

Here is an old post that answers your questions too.

这是一篇旧帖子,也回答了你的问题。

In general it is not a problem at all to have more than one python installation on your machine. You just have to watch out which one you are calling on the command line.

通常,在您的计算机上安装多个python根本不是问题。您只需要在命令行中注意要调用的是哪一个。

>> which python 

... helps to identify where your python binary is located. The original Mac OS X python is usually at "/usr/bin/python"

...有助于确定python二进制文件的位置。最初的Mac OS X python通常位于“/ usr / bin / python”

I personally use the MacPorts python installation. It also supports you with the installation of modules. (see link above)

我个人使用MacPorts python安装。它还支持您安装模块。 (见上面的链接)

#6


-1  

I have 4 versions of python on my MacBook Pro. 2 from the original install of OS X 10.6 and a subsequent update, then self installed copies of python 2.7 and 3.2. You can update the python command to point at any of the versions. They all install in separate directories and cause no problems with each other.

我的MacBook Pro上有4个版本的python。 2.从原来安装的OS X 10.6和随后的更新,然后自行安装python 2.7和3.2的副本。您可以更新python命令以指向任何版本。它们都安装在不同的目录中,彼此之间没有任何问题。

I'm not sure what will happen when you install from a .dmg file. I believe it will simply use whatever version python points to.

我不确定从.dmg文件安装时会发生什么。我相信它只会使用python指向的任何版本。

This post on superuser.com answers your questions on changing default paths.

superuser.com上的这篇文章回答了有关更改默认路径的问题。