配置Conda+Jupyter Notebook+Pycharm

[TOC]

配置Conda+Jupyter Notebook+Pycharm

1. 下載并安裝conda

官網(wǎng)下載即可,如果速度慢可以掛代理或在清華的鏡像源處下載。

我安裝的版本是miniconda。

image

如下圖,注意勾選第一個(gè)選項(xiàng),可以自動(dòng)配置相關(guān)環(huán)境變量;沒有勾選的話需要安裝完成后手動(dòng)配置。

image

執(zhí)行conda --version有正確輸出則說(shuō)明安裝成功。

執(zhí)行conda update -n base conda將base環(huán)境更新至最新版本。

2. 修改conda源or配置代理

由于conda的默認(rèn)源在國(guó)外,所以訪問較慢,有兩種解決方案。

  • 修改源為國(guó)內(nèi)清華tuna鏡像源

    打開C:\Users\songhan\.condarc,覆蓋寫入以下信息:

    channels:
      - defaults
    show_channel_urls: true
    channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
    default_channels:
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
    custom_channels:
      conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
      msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
      bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
      menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
      pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
      simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
    

    再執(zhí)行以下命令:

    conda info  # 查看conda當(dāng)前配置信息
    conda clean -i  # 刪除索引緩存,確保使用的是國(guó)內(nèi)鏡像源
    
  • 第二種方案是配置代理

    打開C:\Users\name\.condarc

    加入以下信息即可

    proxy_servers:
      http: http://addr:port
      https: https://addr:port
    

3. 使用conda創(chuàng)建Python環(huán)境,并安裝jupyter notebook

安裝包時(shí)可能涉及文件讀寫權(quán)限的問題,因此需要以管理員身份打開終端。

權(quán)限問題導(dǎo)致的安裝失敗

image
  • 執(zhí)行以下命令創(chuàng)建python環(huán)境以及安裝jupyter notebook包:

    conda create -name testenv python=3.6
    activate testenv  # 激活剛剛創(chuàng)建的環(huán)境
    conda install -c conda-forge notebook  # 為當(dāng)前環(huán)境安裝jupyter notebook
    

    執(zhí)行jupyter notebook,自動(dòng)跳出瀏覽器界面,安裝成功。(注意要在激活testenv的狀態(tài)下才能使用jupyter)

    image

4. 修改jupyter notebook默認(rèn)啟動(dòng)目錄

修改jupyter notebook默認(rèn)啟動(dòng)目錄之后,每次啟動(dòng)都可以直接定位到需要的目錄。

執(zhí)行jupyter notebook --generate-config,查看配置文件路徑。

image

打開配置文件,定位到## The directory to use for notebooks and kernels.處,修改下一行為目標(biāo)目錄:

c.NotebookApp.notebook_dir = 'D:/0_project/leetcode'  # 注意前面的#要去掉

重新運(yùn)行jupyter,檢查默認(rèn)目錄是否修改成功。

5. Jupyter Notebook進(jìn)一步配置

conda install nb_conda  # 該包將conda創(chuàng)建的環(huán)境自動(dòng)關(guān)聯(lián)到notebook中
conda install pyreadline  # 該包提供代碼補(bǔ)全
conda config --add channels conda-forge  # 添加conda-forge源
conda install jupyterthemes  # 安裝主題包

注意:若想用快捷方式啟動(dòng)jupyter notebook需要將右鍵-屬性-快捷方式-目標(biāo)中的"%USERPROFILE%/"去掉,才能使之前做的啟動(dòng)目錄更改生效。

image

6. 配置Pycharm

用conda安裝了Python環(huán)境之后,按照以下步驟為Pycharm中的項(xiàng)目配置該環(huán)境。

image
image

結(jié)束

到這里就完成了Conda+Jupyter Notebook+Pycharm的基本配置,日常使用中常用命令如下:

conda env list  # 列出系統(tǒng)中所有Python環(huán)境
activate envName  # 激活/切換環(huán)境
conda create -n envName python=3.x  # 新建一個(gè)環(huán)境
conda install packageName  # 安裝新的包
jupyter notebook  # 如果當(dāng)前環(huán)境安裝了jupyter則將其啟動(dòng),否則會(huì)提示未知的指令

參考資料

https://zhuanlan.zhihu.com/p/37093476

https://zhuanlan.zhihu.com/p/56152675

https://www.cnblogs.com/pythonbao/p/10841241.html

https://blog.csdn.net/zhayushui/article/details/80433768

Jupyter 切換各種主題(附圖)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容