首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有最新Python版本的新Conda环境

带有最新Python版本的新Conda环境
EN

Stack Overflow用户
提问于 2022-11-29 09:09:59
回答 1查看 90关注 0票数 2

由于Python版本的更改非常少,所以我总是忘记了如何用最新的Python创建了一个新的Conda环境,用于朱庇特笔记本,所以我想下次我会把它列下来。在StackOverflow中,有一些不再起作用的答案,下面是我在StackOverflow上找到的对我有用的命令的编译(11月29日-2022年)。下面的这些说明适用于Windows,并使用Powershell (尽管它们也可以用于正常的命令行cmd.exe)

代码语言:javascript
复制
    # 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

还有别的办法吗?

EN

回答 1

Stack Overflow用户

发布于 2022-11-29 16:53:36

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

中。

  1. Not理想方法:“快速和肮脏的方法”是在每个env中安装jupyter笔记本。“如果在任何环境中安装jupyter,并在该环境中运行jupyter笔记本,笔记本将使用活动环境中的内核。内核将显示默认名称Python3,但我们可以通过执行以下操作来验证这一功能。”

导入操作系统

打印(os.environ‘’CONDA_DEFAULT_ENV‘)

  1. 惯用的或简单的方法”是“单独注册要在内核列表中显示的每个环境”。不需要安装nb_conda_kernels.

在创建新的env之后,例如python3.11

`

代码语言:javascript
复制
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笔记本时,人们会看到“Python3.11env”作为从中选择的env之一。

注:

这种简单方法的问题是使用!像这样:

代码语言:javascript
复制
!python --version

!pip3 list

!conda list

将始终引用启动jupyter笔记本的env,而不管jupyter笔记本当前选择(正在使用的)什么内核版本。

所以如果我们这样做:

代码语言:javascript
复制
!pip3 install --upgrade numpy

numpy将在启动jupyter笔记本的env中安装或升级。这是一个问题,如果我们正在编程试图升级一个包在jupyter笔记本本身基于条件,例如。

如果我们使用nb_conda_kernels方式,那么上面的命令将始终在活动内核的env中安装/升级,而不管jupyter记事本是从哪个env启动的。

因此,这是值得注意的,如果一个人正在安装包在其他环境中,而不是基础。

就我个人而言,我同时使用了nb_conda_kernels方式(nb_conda_kernels)和通常/简单的方法。只需按照通常的方法执行所有步骤,那么在运行jupyter笔记本之前的最后一步是:

代码语言:javascript
复制
# make sure you are in base env
conda install nb_conda_kernels

因此,我在木星笔记本中的内核列表看起来应该是:

Python 3 (ipykernel)

Python3.11 env

python3.11:python3.11

等。

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

如果我想要!pip3安装-升级来处理基础中的包,同时使用一个版本与基本版本不同的内核(例如Python3.8),我将从基env启动笔记本,并选择内核“Python3.11env”。

如果我需要!pip3安装-升级来处理某个env中的包,我可以从任何env启动笔记本,并选择内核“python3.11:python3.11”。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74611535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档