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

发布于 2024-10-20 21:30:15 字数 1256 浏览 4 评论 0原文

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

我在 OS X 的内置 python 中遇到了一些问题。 (例如,IDLE 不起作用*)。我怀疑我应该安装 更新版本的 python 和 < a href="http://www.python.org/download/mac/tcltk/" rel="nofollow noreferrer">不同版本的 Tk。

我的问题:

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

  2. 我想将终端命令“python”、“ipython”和“easy_install”与最新版本的 python 相关联。我应该怎么做?

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

  4. 有更好的方法吗?

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

*编辑:另外,

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

尽管他现在有了一个可以工作的系统,但我很想请他尝试其他解决方案之一。我不确定我的代码的所有潜在用户是否都能忍受数小时、数千兆字节的安装。

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.

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.

My questions:

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

  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?

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

  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.

*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.

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.

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

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

发布评论

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

评论(7

热风软妹 2024-10-27 21:30:15

https://docs.brew.sh/Homebrew-and-Python 的注释: “Homebrew 提供了用于酿造 Python 3.y 的公式。直到 2019 年底才提供 python@2 公式,但由于 Python 2 弃用,该公式已被删除。”


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

要安装 python ,请尝试以下操作:

brew install python

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

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

我建议您安装pipvirtualenvvirtualenvwrapper 也可以更好地控制您的环境。

Note from https://docs.brew.sh/Homebrew-and-Python: "Homebrew provides formulae to brew Python 3.y. A python@2 formula was provided until the end of 2019, at which point it was removed due to the Python 2 deprecation."


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

To install python try this:

brew install python

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

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.

默嘫て 2024-10-27 21:30:15

您是否尝试过ActivePython

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

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

ActivePython 可以使用 Apple 的 Tcl/Tk 或 ActiveTcl(如果已安装)。

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

  1. 安装 ActivePython 2.7
  2. 打开终端并输入 pypm-2.7 install matplotlib ipython

Have you tried 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.

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.

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

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

  1. Install ActivePython 2.7
  2. Open Terminal and type pypm-2.7 install matplotlib ipython
多像笑话 2024-10-27 21:30:15

几乎在所有情况下,最好使用的 Python 是来自 http://python.org/ 的 Python。它正确设置路径并且不会覆盖任何内容。 DMG 软件包安装通常会自动进行,python setup.py install 也是如此,并且让 setuptools 工作也不是太难。如果您想要按用户安装,可以轻松设置 .pydistutils.cfg 并且 python 会自动识别路径 install_lib = ~/Library/Python/$py_version_short/site-packages代码>

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

玩物 2024-10-27 21:30:15

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

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

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

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

  • 通过 easy_install/pip 为“选定的”Python 安装 Python 软件包(如果它们不可用作端口)。

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

至于您的问题:

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

Q2:easy_install、pip、ipython 将“链接”到安装它们的 python。 (但请参阅提示 1)

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

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

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

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

  2. 为了避免意外,我在我的 ~/.profile 中添加了 echo Selected python is \"$(python_select -s)\" 行,因此我总是知道打开会话时我当前选择的 python...:-)

问候,

Georges

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.

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

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

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

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

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

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.

Q2: easy_install, pip, ipython will be "linked" to the python they were installed by. (but see tip 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.

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

Now, two tips if you try 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.

  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... :-)

Regards,

Georges

千と千尋 2024-10-27 21:30:15

关于 brew 使用的附录:

从一段时间以来,brew install python 将安装 python3

如果你打算安装python2,你想使用

brew install python@2

brew来安装python和python3就完全没问题了!

An addendum regarding the usage of brew:

Since some time, brew install python will install python3.

If you intend to install python2, you want to use

brew install python@2

It is perfectly fine to install both python and python3 using brew!

远昼 2024-10-27 21:30:15

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

一般来说,在你的机器上安装多个 python 根本不是问题。您只需注意在命令行上调用的是哪一个。

>> which python 

...有助于识别您的 python 二进制文件所在的位置。原始的 Mac OS X python 通常位于“/usr/bin/python”,

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

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.

>> which python 

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

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

爱本泡沫多脆弱 2024-10-27 21:30:15

我的 MacBook Pro 上有 4 个版本的 python。 2 从 OS X 10.6 的原始安装和后续更新,然后自行安装 python 2.7 和 3.2 的副本。您可以更新 python 命令以指向任何版本。它们都安装在单独的目录中,并且不会相互造成问题。

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

superuser.com 上的这篇帖子回答您有关更改默认路径的问题。

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.

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.

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

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