新的Conda环境,带有Jupyter Notebook的最新Python版本

qjp7pelc  于 2022-12-02  发布在  Python
关注(0)|答案(1)|浏览(360)

由于Python版本的变化很少,我总是忘记我是如何用最新的Python为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

还有别的办法吗?

wmtdaxz3

wmtdaxz31#

1.上面主要问题中的步骤是nb_conda_kernels方法。在基本环境中安装nb_conda_kernels后,任何从基本环境运行的笔记本电脑都会自动显示任何其他安装了ipykernel的环境中的内核。我们只需要一台jupyter笔记本电脑,最好安装在基本环境中。
1.不是理想的方式:“快速而肮脏的方法”是在每个环境中安装jupyter notebook。“如果你在任何环境中安装jupyter,并在该环境中运行jupyter notebook,notebook将使用活动环境中的内核。内核将显示为默认名称Python 3,但我们可以通过以下操作来验证这一点。”
导入操作系统
打印(操作系统环境['CONDA_DEFAULT_ENV'])
1.“通常或简单的方法”是“单独注册您希望在内核列表中显示的每个环境”。不需要安装nb_conda_kernes。
创建新的env后,以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笔记本时,你会看到“Python 3.11 env”作为一个env来选择。

注意事项:

这种简单方法的问题在于,使用!运行命令,例如:

!python --version

!pip3 list

!conda list

将始终引用启动jupyter notebook的环境,而不管jupyter notebook当前选择(使用)的内核版本是什么。
如果我们这样做:

!pip3 install --upgrade numpy

numpy将在启动jupyter notebook的环境中安装或升级。例如,如果我们试图在jupyter notebook中根据条件升级软件包,这将是一个问题。
如果我们使用nb_conda_kernels的方式,上面的命令将总是在活动内核的env中安装/升级,而不管jupyter notebook是从哪个env启动的。
因此,如果在基础环境之外的环境中安装软件包,这是需要注意的。
个人而言,我使用nb_conda_kernels方式(nb_conda_kernels)和通常的/简单的方式。只要遵循通常方式的所有步骤,那么最后一步,在运行jupyter notebook之前,是:

# make sure you are in base env
conda install nb_conda_kernels

所以我在Jupyter笔记本中的内核列表如下所示:
Python 3(编译器)
Python 3.11环境
Python语言[conda环境:python3.11]
等等。
我可以选择我想要的任何内核,它将工作,同时记住我上面提到的行为。
如果我想让!pip 3 install --upgrade在基础中的包上工作,同时使用一个版本与基础不同的内核(例如Python 3.8),我会从基础env启动notebook,并选择内核“Python3.11 env”。
如果我想让!pip 3 install --upgrade在某个环境的环境中的包上工作,我可以从任何环境启动notebook,并选择内核“Python [conda env:python3. 11]"。

相关问题