由于Python版本的更改非常少,所以我总是忘记了如何用最新的Python创建了一个新的Conda环境,用于朱庇特笔记本,所以我想下次我会把它列下来。在StackOverflow中,有一些不再起作用的答案,下面是我在StackOverflow上找到的对我有用的命令的编译(11月29日-2022年)。下面的这些说明适用于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还有别的办法吗?
发布于 2022-11-29 16:53:36
中。
导入操作系统
打印(os.environ‘’CONDA_DEFAULT_ENV‘)
在创建新的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笔记本时,人们会看到“Python3.11env”作为从中选择的env之一。
注:
这种简单方法的问题是使用!像这样:
!python --version
!pip3 list
!conda list将始终引用启动jupyter笔记本的env,而不管jupyter笔记本当前选择(正在使用的)什么内核版本。
所以如果我们这样做:
!pip3 install --upgrade numpynumpy将在启动jupyter笔记本的env中安装或升级。这是一个问题,如果我们正在编程试图升级一个包在jupyter笔记本本身基于条件,例如。
如果我们使用nb_conda_kernels方式,那么上面的命令将始终在活动内核的env中安装/升级,而不管jupyter记事本是从哪个env启动的。
因此,这是值得注意的,如果一个人正在安装包在其他环境中,而不是基础。
就我个人而言,我同时使用了nb_conda_kernels方式(nb_conda_kernels)和通常/简单的方法。只需按照通常的方法执行所有步骤,那么在运行jupyter笔记本之前的最后一步是:
# 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”。
https://stackoverflow.com/questions/74611535
复制相似问题