如何从 Google Colab 笔记本的“.py”文件中 运行 Python 脚本?
How to run a Python script in a '.py' file from a Google Colab notebook?
%%javascript
IPython.OutputArea.prototype._should_scroll = function(lines) {
return false;
}
%run rl_base.py
我 运行 这个给出错误说 rl_base.py 找不到文件。我已将相同的内容上传到 colab 中的 gdrive,并从我正在 运行ning 我的 .ipynb 文件的同一文件夹中上传,其中包含上述代码
你不应该上传到 gdrive。您应该通过调用
将其上传到 Colab
from google.colab import files
files.upload()
当您从 Google 驱动器 运行 笔记本时,仅为笔记本创建一个实例。要使 Google 驱动器文件夹中的其他文件可用,您可以安装 Google 驱动器:
from google.colab import drive
drive.mount('/content/gdrive')
然后将您需要的文件复制到实例中:
!cp gdrive/My\ Drive/path/to/my/file.py .
并且运行你的脚本:
!python file.py
看来有必要把.py文件名放在""
!python "file.py"
如果您在驱动器的相应文件夹中有 test.py 文件,如下图所示,那么您使用 运行 test.py 文件的命令如上所述下面,
!python gdrive/My\ Drive/Colab\ Notebooks/object_detection_demo-master/test.py
附加信息:
如果你只是想 运行 !python test.py
那么你应该改变目录,通过下面的命令,
%cd gdrive/My\ Drive/Colab\ Notebooks/object_detection_demo-master/
## 1. Check in which directory you are using the command
!ls
## 2.Navigate to the directory where your python script(file.py) is located using the command
%cd path/to/the/python/file
## 3.Run the python script by using the command
!python file.py
一种方法也在使用colabcode.。您将拥有 Visual Studio 代码编辑器的完整 ssh 访问权限。
# install colabcode
!pip install colabcode
# import colabcode
from colabcode import ColabCode
# run colabcode with by deafult options.
ColabCode()
# ColabCode has the following arguments:
# - port: the port you want to run code-server on, default 10000
# - password: password to protect your code server from being accessed by someone else. Note that there is no password by default!
# - mount_drive: True or False to mount your Google Drive
!ColabCode(port=10000, password="abhishek", mount_drive=True)
它将提示您使用 link 到 visual studio 代码编辑器,可以完全访问您的 colab 目录。
这是一个简单的答案和截图
- 安装 google 驱动器
from google.colab import drive
drive.mount('/content/drive')
- 调用py文件路径
import sys
import os
py_file_location = "/content/drive/MyDrive/Colab Notebooks"
sys.path.append(os.path.abspath(py_file_location))
%%javascript
IPython.OutputArea.prototype._should_scroll = function(lines) {
return false;
}
%run rl_base.py
我 运行 这个给出错误说 rl_base.py 找不到文件。我已将相同的内容上传到 colab 中的 gdrive,并从我正在 运行ning 我的 .ipynb 文件的同一文件夹中上传,其中包含上述代码
你不应该上传到 gdrive。您应该通过调用
将其上传到 Colabfrom google.colab import files
files.upload()
当您从 Google 驱动器 运行 笔记本时,仅为笔记本创建一个实例。要使 Google 驱动器文件夹中的其他文件可用,您可以安装 Google 驱动器:
from google.colab import drive
drive.mount('/content/gdrive')
然后将您需要的文件复制到实例中:
!cp gdrive/My\ Drive/path/to/my/file.py .
并且运行你的脚本:
!python file.py
看来有必要把.py文件名放在""
!python "file.py"
如果您在驱动器的相应文件夹中有 test.py 文件,如下图所示,那么您使用 运行 test.py 文件的命令如上所述下面,
!python gdrive/My\ Drive/Colab\ Notebooks/object_detection_demo-master/test.py
附加信息:
如果你只是想 运行 !python test.py
那么你应该改变目录,通过下面的命令,
%cd gdrive/My\ Drive/Colab\ Notebooks/object_detection_demo-master/
## 1. Check in which directory you are using the command
!ls
## 2.Navigate to the directory where your python script(file.py) is located using the command
%cd path/to/the/python/file
## 3.Run the python script by using the command
!python file.py
一种方法也在使用colabcode.。您将拥有 Visual Studio 代码编辑器的完整 ssh 访问权限。
# install colabcode
!pip install colabcode
# import colabcode
from colabcode import ColabCode
# run colabcode with by deafult options.
ColabCode()
# ColabCode has the following arguments:
# - port: the port you want to run code-server on, default 10000
# - password: password to protect your code server from being accessed by someone else. Note that there is no password by default!
# - mount_drive: True or False to mount your Google Drive
!ColabCode(port=10000, password="abhishek", mount_drive=True)
它将提示您使用 link 到 visual studio 代码编辑器,可以完全访问您的 colab 目录。
这是一个简单的答案和截图
- 安装 google 驱动器
from google.colab import drive
drive.mount('/content/drive')
- 调用py文件路径
import sys
import os
py_file_location = "/content/drive/MyDrive/Colab Notebooks"
sys.path.append(os.path.abspath(py_file_location))