关于Colab导入自己的模块的问题
我对 Colab 和 Colab 比较陌生。 python,所以我真的不知道如何使用它。我已将 GitHub 存储库 git 克隆到我的 Colab 笔记本上。我克隆了 GitHub 文件夹内的一些模块,需要将它们导入到笔记本中。例如,我想导入一个 utils 和 models 模块。我运行了代码:
从模型导入* 从实用程序导入*
但是,Colab 编辑器产生了错误 ModuleNotFoundError: No module named 'models'
和 ModuleNotFoundError: No module named 'utils'
。这让我意识到我还没有将 Colab 文件中的 Git Hub 克隆中的模块导入到 Colab 中。我在 StackOverflow、w3schools 或 Colab 官方网站上找不到答案,所以我 在这里提出我的问题。如果你们中的任何人有指南或解决方案的链接,请帮助我。谢谢你!
I am relatively new to Colab & python, so I don't really know how to use it. I have git-cloned a GitHub repository onto my Colab notebook. There are some modules inside the GitHub folder that I have cloned that I need to import them into the notebook. For example, there is a utils and models module that I want to import. I ran the code:
from models import *
from utils import *
However, the Colab editor wrought the error ModuleNotFoundError: No module named 'models'
and ModuleNotFoundError: No module named 'utils'
. This led me to realize that I haven't imported the modules from the Git Hub clone in Colab Files into Colab. I couldn't find an answer on StackOverflow, w3schools, or the Colab official sites so I
wrought my question here. If any of you have a link to a guide or a solution, please help me. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,Jupyter Notebook 的一些技巧确实有效。我使用 %cd pytorch_objectdetecttrack 来访问模块。
Yes, some of the Jupyter Notebook tricks worked. I used %cd pytorch_objectdetecttrack to get to the modules.
不是 Colab 专家,但对于 Python
import
仅适用于 Python 模块。因此,如果您要尝试导入特定脚本,则必须像importfolder.someModule
那样调用它。您尝试访问的文件夹应该首先出现,然后是模块名称。这与
./folder/someModule.py
相同。TL:博士;如果模块不在当前工作目录中,则必须指定模块的路径。
Not an expert in Colab, but for Python
import
only works for Python modules. So if there's a specific script you're trying to import, you will have to call it likeimport folder.someModule
. The folder you're trying to access should go first, then the module name.It's the same as saying
./folder/someModule.py
.tl:dr; You have to specify the path to the module if it's not in the current working directory.