[英]Python magic command %store not working on Google Colab notebook
[英]Using Terminal/Command Line to Execute Python Notebook Shells on Google Colab
我是編程新手,我正在使用 Google Colab 完成“Learning Python the Hard Way”中的練習。
我堅持練習 13,因為我需要使用命令行運行它(即 $ python3.6 ex13.py first 2nd 3rd)。
因此,如果可能,我想使用 Colab 終端在筆記本單元上執行代碼。
如果這不可能,請在您知道的情況下提出其他方法(例如,將代碼保存到谷歌驅動器中的文本文件,並使用終端執行文件)。 我也無法弄清楚這一點。
這就是我卡住的地方:
我想使用 Colab 終端運行此代碼(在這里,我嘗試將文件保存為 Google Drive 中的文本文件,因為我無法使用命令行在筆記本單元格上運行代碼):
保存在 Google Drive 上的 Python 文本文件
這是供您參考的代碼:
# p. 72 - Parameters, Unpacking, Variables
from sys import argv
# read the WYSS section for how to run this
script, first, second, third = argv
print("This script is called:", script)
print("Your first variable is:", first)
print("Your second variable is:", second)
print("Your third variable is:", third)
#This is the command to execute the coder using command line: $ python3.6 ex13.py first 2nd 3rd
但是,我想使用 Google Colab 終端來執行此操作。
要在 shell 中執行,您可以使用!
:
!python ex13.py aaa bbb ccc
或%%shell
魔法:
%%shell
python ex13.py aaa bbb ccc
盡管要使其正常工作,您的ex13.py必須位於您的工作目錄中(或至少可訪問),這可能有點違反直覺:
您的默認工作目錄不是 Google Drive 上存儲 Notebook(和ex13.py )的位置,而是 Colab 臨時會話存儲中的/content
。
因此,首先您要么將ex13.py上傳到/content
(從本地文件系統拖放即可)或將 GDrive 掛載到 Colab(有Mount Drive按鈕),然后使用%cd
將工作目錄更改為腳本的位置%cd
,例如%cd "/content/drive/MyDrive/Colab Notebooks"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.