Python 在 pycharm 中使用 Conda 环境

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

Use Conda environment in pycharm

pythonpycharmconda

提问by Abhinav Rai

Conda env is activated using source activate env_name.

Conda env 使用source activate env_name 激活

How can I activate the environment in pycharm ?

如何在 pycharm 中激活环境?

采纳答案by Ari Cooper-Davis

The best PyCharm specific answer is this one by wasabi(below).

最好的 PyCharm 特定答案是wasabi 的这个答案(如下)。

In general though, if you want to use an interpreter from within a Conda environment then you can change the location of the interpreterto point to the particular environment that you want to use e.g. /home/username/miniconda/envs/bunniesas mentioned in this comment.

但总的来说,如果您想在 Conda 环境中使用解释器,那么您可以更改解释器的位置以指向您想要使用的特定环境,例如,/home/username/miniconda/envs/bunnies本评论中所述

However, as mentioned in this answer by Mark Turner, it is possible to have a shell script executed when activating an environment. This method will not run that shell script, but you can follow his workaround if you need that shell script run:

但是,正如Mark Turner此回答中所提到的,在激活环境时可以执行 shell 脚本。此方法不会运行该 shell 脚本,但如果您需要运行该 shell 脚本,则可以遵循他的解决方法:

  • open a conda prompt
  • activate the environment
  • run pycharm from the conda prompt
  • 打开 conda 提示符
  • 激活环境
  • 从 conda 提示符运行 pycharm

回答by wasabi

open

打开

pycharm/preferences/project/Project Interpreter

pycharm/首选项/项目/项目解释器

And check existing interpreter. Conda environments may already be listed there.

并检查现有的解释器。Conda 环境可能已经在那里列出。

enter image description here

在此处输入图片说明

If not exists, you can create a new conda environment with "Create Conda Env" button

如果不存在,您可以使用“创建 Conda Env”按钮创建一个新的 conda 环境

enter image description here

在此处输入图片说明

If you are looking for a specific conda environment you can use 'add local'. When you click 'add local' you will input conda environment path + /bin/python

如果您正在寻找特定的 conda 环境,您可以使用“添加本地”。当您单击“添加本地”时,您将输入 conda 环境路径 +/bin/python

You can list all conda environment in your system with following commnad.

您可以使用以下命令列出系统中的所有 conda 环境。

>>conda info --env
# conda environments:
#
tensorflow            *  /Users/username/miniconda3/envs/tensorflow

you can chose the approach best fits your needs.

您可以选择最适合您需求的方法。

回答by Mark Turner

As mentioned in one of the comments above, activating an environment can run scripts that perform other actions such as setting environment variables. I have worked in one environment that did this. What worked in this scenario was to:

如上述评论之一所述,激活环境可以运行执行其他操作(例如设置环境变量)的脚本。我曾在一个这样做的环境中工作过。在这种情况下有效的是:

  • open a conda prompt
  • activate the environment
  • run pycharm from the conda prompt
  • 打开 conda 提示符
  • 激活环境
  • 从 conda 提示符运行 pycharm

Pycharm then had access to the environment variables that were set by activating the environment.

Pycharm 然后可以访问通过激活环境设置的环境变量。

回答by Arnaud P

How about environment.yml

怎么样 environment.yml

Pycharm can create a new conda environment indeed. Unfortunately, until this issueis fixed, it won't offer environment.ymlsupport, which means it won't install the dependencies declared there.

Pycharm 确实可以创建一个新的 conda 环境。不幸的是,在修复此问题之前,它不会提供environment.yml支持,这意味着它不会安装在那里声明的依赖项。

When working on a project based on such a file, you need to create / update the dedicated env manually on your machine:

在基于此类文件处理项目时,您需要在您的机器上手动创建/更新专用环境:

conda env create -n <my-project>

Then remember to update each time environment.ymlchanges (from you or upstream).

然后记得在每次environment.yml更改时更新(来自您或上游)。

conda env update -n <my-project>

Not ideal

不理想

回答by TarekB

I had the same problem i am on windows 10 professional 64 bit my solution was to start Pycharm as adminstrator and it worked

我在 Windows 10 Professional 64 位上遇到了同样的问题,我的解决方案是以管理员身份启动 Pycharm 并且它有效

回答by jiripi

It seems important to me to know, that setting project interpreter as described in wasabi's comment does not actually activate the conda environment.

对我来说,重要的是要知道,wasabi 评论中描述的设置项目解释器实际上并没有激活 conda 环境。

I had issue with running xgboost (that I installed with conda) inside PyCharm and it turned out that it also need some folders added to PATH. In the end I had to make do with an ugly workaround:

我在 PyCharm 中运行 xgboost(我用 conda 安装)时遇到问题,结果发现它还需要将一些文件夹添加到 PATH。最后,我不得不使用一个丑陋的解决方法:

  1. Find out what are the additional folders in PATH for given environment (with echo %PATH%in cmd)

  2. In the file I wish to run put to the top before anything else:

  1. 找出给定环境的 PATH 中的其他文件夹是什么(echo %PATH%在 cmd 中)

  2. 在我希望在其他任何事情之前运行的文件中:

import os os.environ["PATH"] += os.pathsep + os.pathsep.join(my_extra_folders_list)

import os os.environ["PATH"] += os.pathsep + os.pathsep.join(my_extra_folders_list)

I know this is not at all proper solution, but i was unable to find any other beside what Mark Turner mentioned in his comment.

我知道这根本不是正确的解决方案,但除了 Mark Turner 在评论中提到的内容之外,我找不到任何其他解决方案。