使用jupyter?notebook保存python代碼為.py格式問題
jupyter notebook保存python代碼為.py格式
Jupyter notebook 源自 Fernando Perez 發(fā)起的 IPython 項(xiàng)目。IPython 是一種交互式 shell,與普通的 Python shell 相似,但具有一些很好的功能(例如語法高亮顯示和代碼補(bǔ)全)。
在jupyter notebook里輸入:
%%writefile train.py train_v = 10 def train_add(list_n): train_sum = 0 for i in range(len(list_n)): train_sum += list_n[i] return train_sum list_n = [2,3,4,5,6] print(train_add(list_n))
%%writefile train.py 表示將文件保存為trian.py的文件,運(yùn)行之后會(huì)顯示:
Writing train.py
%run train.py 20
運(yùn)行%run 表示運(yùn)行某個(gè)python文件
import os os.path.abspath('.') ?#顯示當(dāng)前路勁 'F:\\02.python' os.remove('train.py') ?#移除文件
需要?jiǎng)h除的話,使用remove來刪除。
jupyter notebook代碼補(bǔ)全擴(kuò)展安裝遇到 Jupyter command `jupyter-contrib` not found.”問題
動(dòng)機(jī)
眾所周知,Jupyer notebook 體量小,特別適合初學(xué)者的代碼調(diào)試。其每一個(gè)Cell都可以將歷史的變量保存起來。但是其相對(duì) pycharm 美中不足的就是沒有自帶“代碼補(bǔ)全”的功能,幸好的是有擴(kuò)展來完善。
買了新的電腦,我也想要按照舊電腦的安裝方式安裝該擴(kuò)展—jupyter_contrib_nbextension,但是按照舊的教程安裝不上了,然后找解決方案,嘗試了好幾個(gè)無用的帖子,感覺心累,誤導(dǎo)人,最后找到了解決方案,用作筆記。
其實(shí)該帖子的解決方案不是我解決的,因此要感謝博主“duqiumail”關(guān)于《Jupyter notebook安裝擴(kuò)展工具(nbextensions)提示“Jupyter command jupyter-contrib not found.”》帖子,鏈接在尾部有原始鏈接。
本人使用的python版本為 Anaconda3-2019.10,python37
舊的安裝插件的教程(可以跳過)
windows + R,輸入 cmd
進(jìn)入命令行界面
pip install jupyter_contrib_nbextensions -i https://pypi.mirrors.ustc.edu.cn/simple jupyter contrib nbextension install --user # 這里舊報(bào)錯(cuò)了,Jupyter command `jupyter-contrib` not found,但是舊電腦上沒有報(bào)該錯(cuò)。 pip install --user jupyter_nbextensions_configurator jupyter nbextensions_configurator enable --user
然后進(jìn)入jupyter notebook 后,發(fā)現(xiàn)菜單狼多出來了 Nbextensions
選項(xiàng),然后勾選 Hinterland
勾選上.大功告成。
但是在 jupyter contrib nbextension install --user
的地方舊報(bào)錯(cuò)了。
Jupyter command jupyter-contrib not found.
解決方案——新的安裝方式
1.先使用以下命令
卸載舊版本的 jupyter_contrib_nbextensions 和 upyter_nbextensions_configurator:分別用cmd命令,卸載之前的安裝
pip uninstall jupyter_contrib_nbextensions pip uninstall jupyter_nbextensions_configurator
2.重新下載并安裝:
2.1 先cmd命令安裝
pip install -i http://pypi.douban.com/simple --trusted-host pypi.douban.com jupyter_contrib_nbextensions
2.2 然后 cmd 進(jìn)入 ~Lib\site-packages\jupyter_contrib_nbextensions
目錄
例如
C:\ProgramData\Anaconda3\Lib\site-packages\jupyter_contrib_nbextensions
有時(shí)候會(huì)在用戶文件夾下,如
C:\Users\he\Python\Python38\Lib\site-packages\jupyter_contrib_nbextensions
2.3 在該目錄下,cmd 執(zhí)行下面命令手動(dòng)安裝
python application.py install
2.4 然后安裝
jupyter contrib-nbextension install --user
這時(shí)就不會(huì)提示“Jupyter command jupyter-contrib not found.”這個(gè)問題了
3 接下來繼續(xù)安裝
pip install -i http://pypi.douban.com/simple --trusted-host pypi.douban.com jupyter_nbextensions_configurator jupyter nbextensions_configurator enable --user
這時(shí)從新啟動(dòng)jupyter notebook 就成功看到工具Nbextensions在工具欄了
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)web應(yīng)用框架之增加響應(yīng)對(duì)象
這篇文章主要介紹了python利用web應(yīng)用框架如何增加響應(yīng)對(duì)象的,文中有相應(yīng)的代碼示例,對(duì)大家的學(xué)習(xí)或工作有一定的參考價(jià)值,需要的同學(xué)可以參考下2023-05-05python實(shí)現(xiàn)統(tǒng)計(jì)代碼行數(shù)的方法
這篇文章主要介紹了python實(shí)現(xiàn)統(tǒng)計(jì)代碼行數(shù)的方法,涉及Python中os模塊及codecs模塊的相關(guān)使用技巧,需要的朋友可以參考下2015-05-05使用PyOpenGL繪制三維坐標(biāo)系實(shí)例
今天小編就為大家分享一篇使用PyOpenGL繪制三維坐標(biāo)系實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12Python3.10接入ChatGPT實(shí)現(xiàn)逐句回答流式返回
這篇文章主為大家要介紹了Python3.10接入ChatGPT實(shí)現(xiàn)逐句回答流式返回示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03python?Pydub簡單易用的音頻處理庫使用實(shí)例探索
Pydub是一個(gè)簡單易用的Python庫,它讓音頻處理變得像處理列表或字符串一樣簡單,你可以用Pydub來剪輯、合并、調(diào)整音頻文件,以及執(zhí)行許多其他的音頻處理任務(wù),它支持多種音頻格式,包括常見的MP3、WAV和AAC2024-01-01Python中為feedparser設(shè)置超時(shí)時(shí)間避免堵塞
為feedparser設(shè)置一個(gè)超時(shí)時(shí)間,可是feedparser并沒有提供這個(gè)功能,只好采用其他方法了,感興趣的朋友可以看看2014-09-09