如何在 Mac OSX 10.9 上为 python 3.3.5 安装 NumPy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24615005/
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-08-19 04:52:22  来源:igfitidea点击:

How to install NumPy for python 3.3.5 on Mac OSX 10.9

pythonmacosnumpymatplotlib

提问by DHorowitz

So I'm currently trying to use python so that it will receive an email and open an attachment, and one of the imports I found required was matplotlib.pyplot as plt. This in turn required Numpy and matplotlib, of which matplotlib was easy to import but I can't for the life of me get NumPy to work. I tried going through the repository, unzipping the file, using Xcode, terminal, and homebrew and none of them work. I'm currently continuing to try with terminal but whenever I try to install NumPy it fails. A friend recommended typing in "sudo python setup.py build" followed by "sudo python setup.py install", however an error occurred saying that there was no such file or directory as setup.py. Any way anyone could explain a method of installing NumPy that will work for python 3.3 (Most I see are for 2.7 which I don't have) that won't drive me insane? (In case the question I'm asking seems ridiculously easy to some, I'm only a high school student who's never done anything like this before, so I'm desperately in need of help)

所以我目前正在尝试使用 python 以便它会收到一封电子邮件并打开一个附件,我发现需要的导入之一是 matplotlib.pyplot as plt。这反过来又需要 Numpy 和 matplotlib,其中 matplotlib 很容易导入,但我终生无法让 NumPy 工作。我尝试浏览存储库,解压缩文件,使用 Xcode、终端和自制软件,但它们都不起作用。我目前正在继续尝试使用终端,但是每当我尝试安装 NumPy 时它都会失败。一位朋友建议输入“sudo python setup.py build”,然后输入“sudo python setup.py install”,但是出现错误,说没有setup.py这样的文件或目录。任何人都可以以任何方式解释安装 NumPy 的方法,该方法适用于 python 3.3(我看到的大多数是 2.7,我不知道)没有)这不会让我发疯吗?(如果我问的问题对某些人来说似乎非常简单,我只是一个以前从未做过这样的事情的高中生,所以我非常需要帮助)

采纳答案by tbekolay

Welcome to both Python and Stack Overflow!

欢迎使用 Python 和 Stack Overflow!

Your question is not at all uncommon. I've seen PhD graduates struggle with the exact same issues! While Python is a beautiful programming language with a very friendly community, getting started with the scientific Python stack can be quite a hassle.

你的问题并不少见。我见过博士毕业生在完全相同的问题上挣扎!虽然 Python 是一门优美的编程语言,拥有非常友好的社区,但开始使用科学的 Python 堆栈可能会很麻烦。

There are two nice options for Python on Mac OS X, depending on how much time you want to invest into learning a particular set of tools. Both sets of tools are excellent and are well worth your time, but have a few tradeoffs. They are similar, though, in that they both will require you to spend some time in the terminal; I would recommend installing iTerm2as your first step, if you haven't already.

在 Mac OS X 上,Python 有两个不错的选择,这取决于你想投入多少时间来学习一组特定的工具。两组工具都非常出色,非常值得您花时间,但也有一些权衡。但是,它们的相似之处在于它们都需要您在终端中花费一些时间;如果您还没有安装iTerm2,我建议您将作为第一步安装。

Option 1: Homebrew

选项 1:自制

This option might be a bit more complicated, and may require more time invested up-front, but in the end can save you time and headaches because you have more control and freedom with how you want to set up Python and other command-line tools.

此选项可能有点复杂,并且可能需要预先投入更多时间,但最终可以节省您的时间和麻烦,因为您可以更好地控制和自由地设置 Python 和其他命令行工具.

The first step is to install Homebrew. Currently, this is done with a single terminal command that will guide you through the installation process.

第一步是安装Homebrew。目前,这是通过单个终端命令完成的,该命令将指导您完成安装过程。

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

As part of it, you will have to install XCode (free from the App Store) and its associated command line tools. This is what makes this option time consuming.

作为其中的一部分,您必须安装 XCode(从 App Store 免费)及其相关的命令行工具。这就是使此选项耗时的原因。

Once you've installed Homebrew, you have access to a new command in the terminal, brew. You can use this command to install Python 3, NumPy, and Matplotlib.

安装 Homebrew 后,您可以访问终端中的新命令brew. 您可以使用此命令安装 Python 3、NumPy 和 Matplotlib。

# Install Python 3 that will be managed by Homebrew
brew install python3

# Get access to the scientific Python formulas
brew tap Homebrew/python

# Install Numpy and Matplotlib
brew install numpy --with-python3
brew install matplotlib --with-python3

Then you're good to go! This option gives you access to some powerful tools, like pipand brew. It means that in the future, when you want to install a new Python package, you should be able to pip install <that package>. Other command line tools, like for example git, can be installed with brew install git. It will make programming on Mac OS X a lot easier, in the end!

那你就可以走了!此选项可让您访问一些强大的工具,例如pipbrew。这意味着将来,当您想要安装新的 Python 包时,您应该能够pip install <that package>. 其他命令行工具,例如git,可以使用brew install git. 最终,它将使 Mac OS X 上的编程变得更加容易!

For some more information, see the Homebrew and Python wiki page.

有关更多信息,请参阅Homebrew 和 Python wiki 页面

Option 2: Anaconda

选项 2:蟒蛇

Anaconda is an all-in-one solution that will set up Python and all of the scientific Python tools all at once. All you have to do is download and install it!

Anaconda 是一种一体化解决方案,可以同时设置 Python 和所有科学 Python 工具。您所要做的就是下载并安装它

Once installed, you should be able to run Python code that uses Numpy and Matplotlib. If you need a new Python package, you should be able to open up a terminal and do pip install <that package>. Some command-line tools and libraries are set up to install with conda install <tool>, but not nearly as many packages are available with condaas are available with brew. But, that might not be a big issue -- it depends what you end up using in the future!

安装后,您应该能够运行使用 Numpy 和 Matplotlib 的 Python 代码。如果您需要一个新的 Python 包,您应该能够打开一个终端并执行pip install <that package>. 一些命令行工具和库被设置为使用 安装conda install <tool>,但可用的包数量conda不如brew。但是,这可能不是什么大问题——这取决于您将来最终使用什么!

The quick start guideis a good resource for the most common issues with Anaconda.

快速入门指南是与蟒蛇最常见的问题一个很好的资源。

回答by user3282276

The easiest way would be to use pip (http://en.wikipedia.org/wiki/Pip_(package_manager)) which is a package manager for python packages like apt on Ubuntu or Homebrew on mac (which I would also recommend you get but is not exactly relevant in this situation). Pip will give you a list of python packages that you can search and install from and pip will handle the configuration and update of these packages. Pip by default comes with Python but is not installed but installing pip is very easy, see How do I install pip on macOS or OS X?, all you have to do is run sudo easy_install pipwhich will install pip (This assumes that you already have python installed on your system, if you don't install it before running this command). Then you can use pip to install numpy using sudo pip install numpywhich will install the package numpy. You can also use pip to search packages using pip search <Package Name>which allows you to search through a list of python packages.

最简单的方法是使用 pip ( http://en.wikipedia.org/wiki/Pip_(package_manager)),它是 python 包的包管理器,如 Ubuntu 上的 apt 或 Mac 上的 Homebrew(我也建议你得到但在这种情况下并不完全相关)。Pip 会给你一个 python 包列表,你可以从中搜索和安装,pip 将处理这些包的配置和更新。Pip 默认与 Python 一起提供但未安装但安装 pip 非常简单,请参阅如何在 macOS 或 OS X 上安装 pip?,您所要做的就是运行sudo easy_install pip这将安装 pip (假设您已经在系统上安装了 python,如果您在运行此命令之前没有安装它)。然后你可以使用pip来安装numpysudo pip install numpy这将安装包 numpy。您还可以使用 pip 搜索包,使用pip search <Package Name>它允许您搜索 python 包列表。

Note: I do not have enough reputation to comment on the answer above me but as I have used Anaconda I would like to point out that Anaconda is a full IDE and that installing Anaconda also installs another version of Python on your system which can cause issues when using the shell or running your python programs that require packages provided by Anaconda outside of the IDE.

注意:我没有足够的声誉来评论我上面的答案,但由于我使用过 Anaconda,我想指出 Anaconda 是一个完整的 IDE,并且安装 Anaconda 还会在您的系统上安装另一个版本的 Python,这可能会导致问题当使用 shell 或运行需要由 Anaconda 在 IDE 之外提供的包的 Python 程序时。

回答by NightFury

Installing numpyusing pipdidn't work for me on Mac OSX 10.12.6. It was not detected. Instead I installed using brew, and it worked!

Mac OSX 10.12.6上安装numpyusingpip对我不起作用。它没有被检测到。相反,我使用brew 进行了安装,并且成功了!

>brew install numpy

To verify if it has been installed correctly,

要验证它是否已正确安装,

>python3
>>>import numpy as np

If no error is shown, means it has been installed correctly.

如果没有显示错误,则表示已正确安装。