Jupyter Notebook中配置多版本Python


最新 Anaconda 中,默认安装 Python 3.8.3,因为某些原因需要使用 Python 3.7

1. 安装 Python 3.7

1
2
3
4
# conda 创建一个名为 python37 的环境
conda create --name python37 python=3.7

--name 可简写为 -n

注意: 豆瓣源此处不知为何 404,可使用清华源代替

2.1 切换到 python37 环境

1
2
3
4
5
# Windows 
activate python37

# Mac
source activate python37

注意: PowerShell 不兼容 anaconda 虚拟环境,无法使用 PowerShell 进入 python37
参考: https://blog.csdn.net/qq_44671752/article/details/104277948
可使用 cmd

2.2 将当前Python版本(python37)加入现有的 Jupyter

1
python -m ipykernel install --name python37

出现问题: D:337.exe: No module named ipykernel

解决:

1
pip install ipykernel

重新运行
通过ipykernel为jupyter添加 python37 环境

1
2
3
# 其实 --name 只是指定一个在 Jupyter 的 display_name 而已,一定要确保当前已切换到 python37 环境
# 调用的是 python ,而 python 具体调用谁 取决于 当前处于哪个环境
python -m ipykernel install --name python37

注意:不是下方这句, 下方是为 单独用户安装,配置文件路径不一致 并且 你需要将哪个环境的python 添加到 Jupyter 中,就要切换到哪个环境,再执行 此条语句将 ipykernel 指向当前环境 且 注册到 Jupyter 中

执行上方后,就会导致下图所示,解决:将其修改为默认的路径即可

1
D:\\anaconda3\\python.exe

通过下方命令 可以看出 python 在哪个环境

成功为 jupyter 添加 python37 环境后,检查

可以打开
C:\Users\yiyun\AppData\Roaming\jupyter\kernels\python3
C:\ProgramData\jupyter\kernels\python37
查看 kernel.json 中 Python 路径是否配置正确

3.离开 python37 环境

1
2
3
4
5
# Windows
deactivate

# Mac
source deactivate

4. 重启 Jupyter

查看 Kernel 是否有 python37 可供选择

打开ipynb 后 弹出错误

解决: 缺少包, 安装包

1
2
3
activate python37

pip install autopep8

PS: 目前新环境,还没安装任何包,需要安装包

报错,不一定是 python 版本问题,也可能是 包版本问题

AttributeError: 'DataFrame' object has no attribute 'as_matrix'解决方法 pandas 新版本中移除了 as_matrix

解决方法1:

1
2
3
pip uninstall pandas

pip install pandas==0.25.3

解决方法2:
使用新 api 代替

1
2

df.values

补充

1. 显示配置源

1
conda config --show channels
1
conda config --show-sources

C:\.condarc

1
2
3
4
5
6
7
8
9
ssl_verify: true
channels:
- https://pypi.douban.com/simple/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- defaults
show_channel_urls: true

2. 临时使用国内源

pip install 临时使用国内源可用 -i

1
pip install keras==2.0.8 -i https://pypi.douban.com/simple/ 

3. conda 多环境管理

3.1 conda 查看已有环境

1
conda info --envs

3.2 克隆环境

1
conda create -n py37copyed --clone python37

3.3 删除环境

1
conda remove -n <env_name> --all

3.4 分享环境

方式1: conda
1
2
3
4
5
6
7
8
# 导出环境: 导出当前环境 到 environment.yml
conda env export --name environmentName > environment.yml

# 使用环境方式1: 根据此文件 创建环境
conda env export > environment.yml

# 使用环境方式2: 将目标环境更新为 与 源环境完全一致
conda env update -f environment.yml
方式2: pip
1
2
3
4
5
# 生成 requirements.txt 文件
pip freeze > requirements.txt

# 安装 requirements.txt 文件依赖
pip install -r requirements.txt

安装的包被存于 D:337-packages

3.5 切换环境

1
2
3
4
activate python37

# 第一个命令无效时使用
conda activate python37

离开环境

1
conda deactivate

4. Jupyter Notebook Kernel 管理

4.1 查看安装的内核和位置

1
jupyter kernelspec list

C:3 为 anaconda3 默认安装后 jupyter配置文件地址
kernel.json

1
2
3
4
5
6
7
8
9
10
11
{
"argv": [
"D:\\anaconda3\\python.exe",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3",
"language": "python"
}

4.2 移除不需要的kernel

1
jupyter kernelspec remove base

参考

感谢帮助!


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM