简体   繁体   English

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

[英]What is the best way to install python 2 on OS X?

A colleague of mine wants to use my python 2 code on his OS X (10.6) machine. 我的一位同事希望在他的OS X(10.6)机器上使用我的python 2代码。 My code imports several built-in python packages, including Tkinter and shelve, and also uses third-party packages, including numpy, scipy, matplotlib, and ipython. 我的代码导入了几个内置的python包,包括Tkinter和shelve,还使用了第三方包,包括numpy,scipy,matplotlib和ipython。

I've encountered a few problems with OS X's built-in python. 我在OS X的内置python中遇到了一些问题。 (IDLE doesn't work, for example*). (IDLE不起作用,例如*)。 I suspect I should install a more recent version of python , and a different version of Tk . 我怀疑我应该安装更新版本的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. 我想将终端命令'python','ipython'和'easy_install'与更新版本的python相关联。 How should I do this? 我该怎么做?

  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. 我的同事尝试了MacPorts,它似乎运行良好,但有几个speedbumps。 First we had to install Xcode from the system install disk. 首先,我们必须从系统安装盘安装Xcode。 This is not a fast or lightweight install (several GB). 这不是快速或轻量级安装(几GB)。 Luckily we still had the disk! 幸运的是我们还有磁盘! Once Xcode was installed, MacPorts was easy to install. 安装Xcode后,MacPorts易于安装。 Python and the python subpackages we needed were also easy to install, but he told me this installation took several hours. Python和我们需要的python子包也很容易安装,但他告诉我这个安装需要几个小时。 Presumably this delay is due to compilation? 据推测,这种延迟是由于汇编造成的? He had an easy time setting the MacPorts python as default. 他很容易将MacPorts python设置为默认值。 However, I think we have to change the 'Python Launcher' application by hand, this seems to still default to the system 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. 我不确定我的所有代码的潜在用户是否会容忍多小时,数千兆字节的安装。

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. 我建议你安装pipvirtualenvvirtualenvwrapper来更好地控制你的环境。

Have you tried ActivePython ? 你试过ActivePython吗?

It includes a package manager ( PyPM ) that, by default, installs into your home directory (eg: ~/Library/Python/2.7 ). 它包括一个包管理器( PyPM ),默认情况下,它安装在您的主目录中(例如: ~/Library/Python/2.7 )。 Main scripts get symlinked in /usr/local/bin ; 主脚本在/usr/local/bin符号链接; use the included pythonselect to set the active Python version. 使用包含的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 安装ActivePython 2.7
  2. Open Terminal and type pypm-2.7 install matplotlib ipython 打开终端并键入pypm-2.7 install matplotlib ipython

An addendum regarding the usage of brew : 关于brew使用的附录:

Since some time, brew install python will install python 3 . 从一段时间以来, brew install python将安装python 3

If you intend to install python 2 , you want to use 如果你打算安装python 2 ,你想使用

brew install python@2

It is perfectly fine to install both python and python3 using brew! 使用brew安装python和python3完全没问题!

In almost all cases, the best python to use is the one from http://python.org/ . 在几乎所有情况下,最好使用的python是来自http://python.org/的 python。 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. DMG软件包安装通常会自动工作, python setup.py install ,并且让setuptools工作并不困难。 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 如果你想要每个用户安装,很容易设置.pydistutils.cfg和python自动识别路径install_lib = ~/Library/Python/$py_version_short/site-packages

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. Q1:使用MacPorts,没有。 while not a frequent user, I've installed and used matplotlib in 2.6 and 2.7, switching between the two using python_select. 虽然不是常用的用户,但我在2.6和2.7中安装并使用了matplotlib,使用python_select在两者之间切换。

Q2: easy_install, pip, ipython will be "linked" to the python they were installed by. Q2:easy_install,pip,ipython将“链接”到他们安装的python。 (but see tip 1) (但见提示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下pip install numpy 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. MacPorts在OS X安装中与/ opt / local目录中分别安装端口,每个python版本都安装在/opt/local/Library/Frameworks/Python.framework/Versions/{2.5,2.6,2.7,...}目录。 Using python_select cleanly switch the "python" command using links. 使用python_select干净地使用链接切换“python”命令。 BUT... the Versions/{2.5,2.6,2.7,...}/bin directory, where python scripts are installed, is not added to the PATH. 但是......安装了python脚本的Versions/{2.5,2.6,2.7,...}/bin目录没有添加到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. 只需添加: 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 Selected python is \\"$(python_select -s)\\"行到我的~/.profile ,所以我总是知道在打开会话时哪个是我当前选择的python ...: - )

Regards, 问候,

Georges 乔治

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. 通常,在您的计算机上安装多个python根本不是问题。 You just have to watch out which one you are calling on the command line. 您只需要注意在命令行中调用哪一个。

>> which python 

... helps to identify where your python binary is located. ...有助于识别python二进制文件的位置。 The original Mac OS X python is usually at "/usr/bin/python" 最初的Mac OS X python通常位于“/ usr / bin / python”

I personally use the MacPorts python installation. 我个人使用MacPorts python安装。 It also supports you with the installation of modules. 它还支持您安装模块。 (see link above) (见上面的链接)

I have 4 versions of python on my MacBook Pro. 我的MacBook Pro上有4个版本的python。 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. 2.从原来安装的OS X 10.6和随后的更新,然后自行安装python 2.7和3.2的副本。 You can update the python command to point at any of the versions. 您可以更新python命令以指向任何版本。 They all install in separate directories and cause no problems with each other. 它们都安装在不同的目录中,彼此之间没有任何问题。

I'm not sure what will happen when you install from a .dmg file. 我不确定从.dmg文件安装时会发生什么。 I believe it will simply use whatever version python points to. 我相信它只会使用python指向的任何版本。

This post on superuser.com answers your questions on changing default paths. superuser.com上的这篇文章回答了有关更改默认路径的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM