Python 让 PyC​​harm 识别 Anaconda 的 SciPy

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

Getting PyCharm to recognize Anaconda's SciPy

pythonscipypycharmanaconda

提问by hillel

I need to use the SciPy libraries inside the PyCharm IDE (on a Mac OSX Lion machine). The SciPy website writes that the simplest installation method for Mac users is to install Anaconda (or an equivalent distro). I used the Anaconda installer, and it created an anaconda directory in my home folder, where I find a lib/python2.7/site-packagesdirectory with the required packages. However, PyCharm is not aware of all this and the SciPy import statements remain unresolved.

我需要在 PyCharm IDE 中使用 SciPy 库(在 Mac OSX Lion 机器上)。SciPy 网站写道,Mac 用户最简单的安装方法是安装 Anaconda(或等效发行版)。我使用了 Anaconda 安装程序,它在我的主文件夹中创建了一个 anaconda 目录,在那里我找到了一个lib/python2.7/site-packages包含所需软件包的目录。然而,PyCharm 并不知道这一切,SciPy 导入语句仍未解决。

My question is then how to make PyCharm work with Anaconda?

我的问题是如何让 PyC​​harm 与 A​​naconda 一起工作?

采纳答案by hillel

I'm still coming to terms with the Python ecosystem and PyCharm, so take the following with a grain of salt, but after reading up a bit, I thought I'd write a detailed explanation.

我仍然在接受 Python 生态系统和 PyCharm,所以对以下内容持保留态度,但在阅读了一些之后,我想我会写一个详细的解释。

During installation, Anaconda changes the default Python interpreter to ~/anaconda/bin/python. This interpreter is configured with a sys.paththat defaults to the libraries in ~/anaconda/lib . Package managers like python's pip use the interpreter that's running them to determine the path in which to install packages, so after Anaconda is installed, all packages installed via pip or other methods will be placed somewhere inside ~/anaconda/lib. i.e. even without using something like virtualenv, every Python interpreter has its own ecosystem and running pip with different interpreters will install packages into different directories.

在安装过程中,Anaconda 将默认 Python 解释器更改为 ~/anaconda/bin/python。这个解释器配置了一个sys.path默认为 ~/anaconda/lib 中的库。像 python 的 pip 这样的包管理器使用运行它们的解释器来确定安装包的路径,所以在安装 Anaconda 之后,所有通过 pip 或其他方法安装的包都将放置在 ~/anaconda/lib 中的某个位置。即,即使不使用类似 virtualenv 的东西,每个 Python 解释器都有自己的生态系统,使用不同的解释器运行 pip 会将包安装到不同的目录中。

PyCharm handles all this in the Project Settings-->Project Interpreter-->Python Interpretersscreen. To make PyCharm aware of the Anaconda distribution, you need to add the Anaconda python interpreter to the Project Interpreter-->Python Interpreterslist and make it the default for the project. PyCharm will then locate all packages installed in Anaconda's interpreter ecosystem (~/anaconda/lib) and list them under packagesin the lower pane. It will also prompt you to install setup_tools and pip for that interpreter, and once you do that you'll be able to use the installbutton in the lower pane to add more packages to the Anaconda ecosystem.

PyCharm 在Project Settings-->Project Interpreter-->Python Interpreters屏幕上处理所有这些。要让 PyC​​harm 知道 Anaconda 发行版,您需要将 Anaconda python 解释器添加到 Project Interpreter-->Python Interpreters列表中并使其成为项目的默认值。然后 PyCharm 将找到安装在 Anaconda 解释器生态系统 (~/anaconda/lib) 中的所有包,并packages在下方窗格中列出它们。它还会提示您为该解释器安装 setup_tools 和 pip,一旦您这样做,您就可以使用install下方窗格中的按钮向 Anaconda 生态系统添加更多包。

Once you've added the Anaconda interpreter, you can also use the virtualenv button from the toolbar on the upper pane to create a virtualenv that inherits from the Anaconda interpreter's environment. That way you can install new packages in a way that would not affect the global Anaconda distribution.

添加 Anaconda 解释器后,您还可以使用上方窗格工具栏中的 virtualenv 按钮创建一个从 Anaconda 解释器环境继承的 virtualenv。这样您就可以以不影响全球 Anaconda 发行版的方式安装新软件包。