Python 在 PyCharm 中使用 (Ana)conda

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

Using (Ana)conda within PyCharm

pythonvirtualenvpycharmanacondaconda

提问by memilanuk

I've got Pycharm 4 running on my Linux (Ubuntu 14.04) machine. In addition to the system python, I've also got Anaconda installed. Getting the two to play nicely together seems to be a bit of a problem... PyCharm provides some interesting integration for virtualenvs and pip, but the Anaconda Python distribution seems to prefer using its own condatool for both activities.

我的 Linux (Ubuntu 14.04) 机器上运行着 Pycharm 4。除了系统python,我还安装了Anaconda。让两者很好地一起玩似乎有点问题……PyCharm 为virtualenvs 和提供了一些有趣的集成pip,但 Anaconda Python 发行版似乎更喜欢conda在这两个活动中使用自己的工具。

Is there a relatively simple/painless way to be able to use condain conjunction with PyCharm? Not just as an alternative interpreter i.e. point PyCharm at the Anaconda Python binary for a project interpreter, but to be able to create, source/activate and deactivate virtual envs, add/remove packages in those virtual envs, etc.

是否有一种相对简单/无痛的方法可以conda与 PyCharm 结合使用?不仅作为替代解释器,即在项目解释器的 Anaconda Python 二进制文件中指向 PyCharm,而且能够创建、源/激活和停用虚拟环境,在这些虚拟环境中添加/删除包等。

Or am I going to have to choose between using Anaconda (and having a more recent and up-to-date python than may come with the system), and being able to use PyCharm's features to their fullest extent?

或者我是否必须在使用 Anaconda(并拥有比系统附带的更新和最新的 Python)和能够最大程度地使用 PyCharm 的功能之间做出选择?

采纳答案by cyberbikepunk

I know it's late, but I thought it would be nice to clarify things: PyCharm and Conda and pipwork well together.

我知道已经晚了,但我认为澄清一些事情会很好:PyCharm 和 Conda以及 pip可以很好地协同工作。

The short answer

简短的回答

Just manage Conda from the command line. PyCharm will automatically notice changes once they happen, just like it does with pip.

只需从命令行管理 Conda 。一旦发生变化,PyCharm 会自动注意到变化,就像pip 一样

The long answer

长答案

Create a new Conda environment:

创建一个新的 Conda 环境:

conda create --name foo pandas bokeh

conda create --name foo pandas bokeh

This environment lives under conda_root/envs/foo. Your python interpreter is conda_root/envs/foo/bin/pythonX.Xand your all your site-packages are in conda_root/envs/foo/lib/pythonX.X/site-packages. This is same directory structure as in a pip virtual environement. PyCharm sees no difference.

这种环境生活在conda_root/envs/foo. 你的 python 解释器是conda_root/envs/foo/bin/pythonX.X,你所有的站点包都在conda_root/envs/foo/lib/pythonX.X/site-packages. 这与 pip 虚拟环境中的目录结构相同。PyCharm 认为没有区别。

Now to activate your new environment from PyCharm go to file > settings > project > interpreter, select Add localin the project interpreter field (the little gear wheel) and hunt down your python interpreter. Congratulations! You now have a Conda environment with pandas and bokeh!

现在要从 PyCharm 激活您的新环境,请转到文件 > 设置 > 项目 > 解释器,在项目解释器字段(小齿轮)中选择添加本地并找到您的 Python 解释器。恭喜!您现在有一个带有熊猫和散景的 Conda 环境!

Now install more packages:

现在安装更多软件包:

conda install scikit-learn

conda install scikit-learn

OK... go back to your interpreter in settings. Magically, PyCharm now sees scikit-learn!

好的...返回设置中的解释器。神奇的是,PyCharm 现在可以看到 scikit-learn!

And the reverse is also true, i.e. when you pip install another package in PyCharm, Conda will automatically notice. Say you've installed requests. Now list the Conda packages in your current environment:

反过来也是如此,即当您在画中画安装PyCharm另一个包,康达会自动通知。假设您已经安装了请求。现在列出当前环境中的 Conda 包:

conda list

conda list

The list now includes requests and Conda has correctly detected (3rd column) that it was installed with pip.

该列表现在包括请求,并且 Conda 已正确检测到(第 3 列)它是通过 pip 安装的。

Conclusion

结论

This is definitely good news for people like myself who are trying to get away from the pip/virtualenv installation problems when packages are not pure python.

对于像我这样的人来说,当软件包不是纯 python 时,他们试图摆脱 pip/virtualenv 安装问题,这绝对是个好消息。

NB:I run PyCharm pro edition 4.5.3 on Linux. For Windows users, replace in command linewith in the GUI(and forward slashes with backslashes). There's no reason it shouldn't work for you too.

注意:我在 Linux 上运行 PyCharm 专业版 4.5.3。对于Windows用户,取代在命令行中使用的图形用户界面(与反斜杠正斜杠)。没有理由它也不适合你。

EDIT:PyCharm5 is out with Conda support! In the community edition too.

编辑:PyCharm5 支持 Conda!在社区版中也是如此。

回答by SpeedCoder5

Continuum Analytics now provides instructions on how to setup Anaconda with various IDEs including Pycharm here. However, with Pycharm 5.0.1 running on Unbuntu 15.10 Project Interpreter settings were found via the File | Settings and then under the Project branch of the treeview on the Settings dialog.

连续Analytics(分析)现在提供各种集成开发环境,包括Pycharm如何设置蟒蛇说明这里。但是,在 Unbuntu 15.10 上运行 Pycharm 5.0.1 时,可以通过 File | 找到 Project Interpreter 设置。设置,然后在“设置”对话框上的树视图的“项目”分支下。

回答by BullyWiiPlaza

Change the project interpreter to ~/anaconda2/python/binby going to File -> Settings -> Project -> Project Interpreter. Also update the run configuration to use the project default Python interpreter via Run -> Edit Configurations. This makes PyCharmuse Anacondainstead of the default Python interpreter under usr/bin/python27.

~/anaconda2/python/bin转到将项目解释器更改为File -> Settings -> Project -> Project Interpreter。同时更新运行配置以通过Run -> Edit Configurations. 这使得PyCharm使用Anaconda而不是默认的Python解释器下usr/bin/python27

回答by Piotr Czapla

as per @cyberbikepunk answer pycharm supports Anaconda since pycharm5!

根据@cyberbikepunk 的回答,pycharm 从 pycharm5 开始就支持 Anaconda!

Have a look how easy is to add an environment: enter image description here

看看添加环境是多么容易: 在此处输入图片说明

回答by noviceprogrammer

this might be repetitive. I was trying to use pycharm to run flask - had anaconda 3, pycharm 2019.1.1 and windows 10. Created a new conda environment - it threw errors. Followed these steps -

这可能是重复的。我试图使用 pycharm 运行烧瓶 - 有 anaconda 3、pycharm 2019.1.1 和 Windows 10。创建了一个新的 conda 环境 - 它抛出了错误。遵循以下步骤 -

  1. Used the cmd to install python and flask after creating environment as suggested above.

  2. Followed thisanswer.

  3. As suggested above, went to Run -> Edit Configurations and changed the environment there as well as in (2).
  1. 按照上面的建议创建环境后,使用cmd安装python和flask。

  2. 按照这个答案。

  3. 如上所述,转到“运行”->“编辑配置”并更改那里以及 (2) 中的环境。

Obviously kept the correct python interpreter (the one in the environment) everywhere.

显然到处都保留了正确的python解释器(环境中的那个)。