简体   繁体   English

在 Conda 环境中安装 python 内核

[英]Installing python kernel in a Conda environment

I have just started using jupyter notebook for my development process.我刚刚开始在我的开发过程中使用 jupyter notebook。 I started by creating a new python environment:我首先创建了一个新的 python 环境:

$ conda create -n testenv

Then I activate it:然后我激活它:

$ source activate testenv

And install python kernel module:并安装 python 内核模块:

$ pip install ipykernel

Now that's when the fuzziness begins.现在就是模糊开始的时候。 I want to create a new kernel specifically to my active environment only.我只想专门为我的活动环境创建一个新内核。 Following documentation, I did:根据文档,我做了:

$ python -m ipykernel install --user --name testenv --display-name "Python (testenv)"

With this I believe I have just created a new Python kernel for the testenv environment to be used within Jupyter Notebook.有了这个,我相信我刚刚为在 Jupyter Notebook 中使用的testenv环境创建了一个新的 Python 内核。 Now, I want to confirm this information and I check:现在,我想确认此信息并检查:

$ jupyter kernelspec list

Available kernels:
testenv   /home/{{user}}/.local/share/jupyter/kernels/testenv
python2   /home/{{user}}/miniconda2/share/jupyter/kernels/python2

$ conda env list
# conda environments:
#
base         /home/{{user}}/miniconda2
testenv   *  /home/{{user}}/miniconda2/envs/testenv

I was expecting to see my kernel installed within the testenv environment, something like:我期待看到我的内核安装在testenv环境中,例如:

/home/{{user}}/miniconda2/envs/testenv/kernels/testenv

I am failing to see how do the environments and kernels tie together and how can I confirm this information.我没有看到环境和内核如何联系在一起,以及如何确认这些信息。

The problem is that when you create the empty environment, it installs absolutely no packages, even pip and Python.问题在于,当您创建空环境时,它绝对不会安装任何包,甚至是 pip 和 Python。 Therefore, when you use pip to install ipykernel , you're using the pip from the base environment.因此,当您使用 pip 安装ipykernel ,您使用的是基础环境中的 pip。 You need to create the environment with pip and python需要用pippython创建环境

conda create -n testenv python

You can check this by typing您可以通过键入来检查这一点

which pip

after you create the blank environment.创建空白环境后。

Finally, you should use conda to install all packages, including ipykernel, if at all possible.最后,你应该使用conda来安装所有的软件包,包括ipykernel,如果可能的话。

First of all you need to use the following:首先,您需要使用以下内容:

conda create -n testenv

After you should use:在你应该使用之后:

conda activate testenv
conda install ipykernel
python3 -m ipykernel install --user --name condaenv --display-name "Python3 (testenv)"

Once you have done this, you could start up the notebook by using jupyter notebook and opens any .ipynb notebook.完成此操作后,您可以使用jupyter notebook启动jupyter notebook并打开任何.ipynb notebook。 Inside that notebook, select the menu Kernel > Change kernel > Python3 (condaenv) to activate the conda environment kernel.在该笔记本中,选择菜单 Kernel > Change kernel > Python3 (condaenv) 以激活 conda 环境内核。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM