新的 Conda 环境以及适用于 Jupyter Notebook 的最新 Python 版本

2024-02-10

由于 Python 版本变化很少,我总是忘记如何使用最新的 Python for Jupyter Notebook 创建新的 Conda 环境,所以我想下次将其列出来。从 StackOverflow 来看,有一些答案不再有效,下面是我在 StackOverflow 上找到的对我有用的命令汇编,2022 年 11 月 29 日。以下说明适用于 Windows,并使用 Powershell(尽管它们也可用于普通命令行 cmd.exe)

    # make sure you are in the base env

    # update conda
    conda update conda

    # to allow support for powershell
    conda init --all

    # The conda-forge repository seems to have at least the latest
    # stable Python version, so we will get Python from there.
    # add conda-forge to channels of conda.
    conda config --add channels conda-forge

    conda update jupyter
    # to fix 500 internal server error when trying to open a notebook later
    pip3 install --upgrade --user nbconvert

    # nb_conda_kernels enables a Jupyter Notebook or JupyterLab
    # application in one conda environment to access kernels for Python,
    # R, and other languages found in other environments.

    conda install nb_conda_kernels

    # I will now create a new conda env for Python 3.11 and name it as Python3.11
    conda create -n python3.11 python=3.11

    # check that it was created
    conda info --envs

    conda activate python3.11

    # Once installed, need to install ipykernel so Jupyter notebook can
    # see the new environment python3.11. 
    conda install -n python3.11 ipykernel

    # install ipywidgets as well for some useful functionalities 
    conda install -n python3.11 ipywidgets

    # Since I use R too, I'll also add a note here on R
    # To utilize an R environment, it must have the r-irkernel package; e.g.
    # conda install -n r_env r-irkernel

    # example to install a package in the new env, if desired
    # conda install --update-all --name python3.11 numpy

    #conda list will show the env's packages, versions, and where they came from too
    conda activate python3.11
    conda list
    conda deactivate

    # Now to check if the new environment can be selected in Jupyter
    # Notebook.  I change to the root directory first so jupyter 
    # notebook can see every folder.  Note that we are in base
    # environment, although no problem if in another environment 
    cd\
    jupyter notebook

    # If I open an existing notebook for example, I can tap on Kernel,
    # then Change kernel, and I should now be able to select the kernel
    # from the new environment I created, shown as "Python [conda env:python3.11]".
    #
    # There will also be another entry showing just the name of the env,
    # in this case, python3.11.  Just ignore this, select the entries
    # starting with "Python [conda env" ...   
    # 
    # If I tapped on New instead when Jupyter Notebook opened, it will
    # also show the list of envs. 

    # to check version, either use :
    !python --version

    # or

    from platform import python_version
    print(python_version()) 

    # both will show the Python version of whatever kernel is in use
    # by Jupyter notebook

    # to test Python 3.10 or 3.11 for example... from 3.10, an optional
    # strict parameter for zip has been added and can be used to
    # generate an error if lists' lengths are not the same

    a = [1,2,3,4]
    b = ['a', 'b', 'c']
    for val1, val2 in zip(a,b, strict = True):
        print(val1, val2)
    
    # this should appear - ValueError: zip() argument 2 is shorter than argument 1

还有别的办法吗?


  1. 上面主要问题的步骤是nb_conda_kernels 方式。在基本环境中安装 nb_conda_kernels 后,从基本环境运行的任何笔记本都会自动显示安装了 ipykernel 的任何其他环境中的内核。我们只需要一台 jupyter 笔记本,最好安装在基础环境中。

  2. 不是理想的方式:“快速而肮脏的方法”是在每个环境中安装jupyter笔记本。 “如果您在任何环境中安装 jupyter 并从该环境运行 jupyter 笔记本,笔记本将使用活动环境中的内核。内核将以默认名称 Python 3 显示,但我们可以通过执行以下操作来验证其是否有效。”

导入操作系统

打印(os.environ['CONDA_DEFAULT_ENV'])

  1. The "通常或简单的方法”是“单独注册您想要在内核列表中显示的每个环境。”无需安装 nb_conda_kernels。

创建新环境后,例如python3.11

`

conda activate python3.11  # make it active

conda install ipykernel    # needed for each env

conda install ipywidgets   # for additional jupyter functionalities

python -m ipykernel install --user --name python3.11 --display-name "Python 3.11 env"            # will install kernelspec python3.11

`

就是这样,当运行 jupyter Notebook 时,人们会看到“Python 3.11 env”作为可供选择的环境之一。

NOTE:

这种简单方法的问题是使用 !喜欢:

!python --version

!pip3 list

!conda list

将始终引用 jupyter Notebook 启动的环境,无论 jupyter Notebook 当前选择(正在使用)的内核版本是什么。

所以如果我们这样做:

!pip3 install --upgrade numpy

numpy 将在启动 jupyter Notebook 的环境中安装或升级。例如,如果我们尝试根据条件以编程方式升级 jupyter Notebook 本身内的包,这就是一个问题。

相反,如果我们使用 nb_conda_kernels 方式,则上述命令将始终在活动内核的环境中安装/升级,无论 jupyter Notebook 是从哪个环境启动的。

因此,如果在除基础环境之外的环境中安装软件包,则需要注意这一点。

就我个人而言,我使用 nb_conda_kernels 方式(nb_conda_kernels)和通常/简单的方式。只需按照通常方式的所有步骤进行操作,然后在运行 jupyter notebook 之前的最后一步是:

# make sure you are in base env
conda install nb_conda_kernels

所以我在 Jupyter Notebook 中的内核列表如下所示:

Python 3(ipykernel)

Python3.11环境

Python [conda 环境:python3.11]

etc.

我可以选择我想要的任何内核,它会工作,同时记住我上面提到的行为。

如果我希望 !pip3 install --upgrade 在基础中的包上工作,同时使用版本与基础不同的内核(例如 Python 3.8),我将从基础环境启动笔记本,并选择内核“Python3” .11环境”。

如果我希望 !pip3 install --upgrade 在某个环境的环境中的包上工作,我可以从任何环境启动笔记本,并选择内核“Python [conda env:python3.11]”。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

新的 Conda 环境以及适用于 Jupyter Notebook 的最新 Python 版本 的相关文章

随机推荐