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