简体   繁体   English

导入错误:在 Colab google 中没有名为 ... 的模块

[英]ImportError: No module named ... in Colab google

I'm following the tutorial here (Object Detection in Google Colab with Custom Dataset).我正在关注此处的教程(使用自定义数据集在 Google Colab 中进行对象检测)。 The first line of the notebook is a git clone of the tensorflow models:笔记本的第一行是 tensorflow 模型的 git 克隆:

!git clone --quiet https://github.com/tensorflow/models.git

After, they set the PYTHONPATH variable to be sure we can import models.之后,他们设置了 PYTHONPATH 变量以确保我们可以导入模型。

os.environ['PYTHONPATH'] += ':/content/models/research/:/content/models/research/slim/'

If I try at this stage to import a model如果我在这个阶段尝试导入模型

from nets import inception_resnet_v2 

I get the error:我收到错误:

ImportError: No module named nets

I checked and nets folder and nets/inception_resnet_v2.py file are there (in models/research/slim folder).我检查了 nets 文件夹和 nets/inception_resnet_v2.py 文件(在 models/research/slim 文件夹中)。 I suspect that it's related to the colab naming convention because the pwd command gives:我怀疑它与 colab 命名约定有关,因为 pwd 命令给出:

/root/models/research

I substituted content for root in the above command but it does not work either.我在上面的命令中替换了 root 的内容,但它也不起作用。 Someone here posted a similar question but the only answer refers to tensorflow issue 1832 which is not the problem here. 这里有人发布了一个类似的问题,但唯一的答案是 tensorflow 问题1832 ,这不是这里的问题。 Can someone help?有人可以帮忙吗?

EDIT: operating system is Linux-4.14.79+-x86_64-with-Ubuntu-18.04-bionic编辑:操作系统是 Linux-4.14.79+-x86_64-with-Ubuntu-18.04-bionic

The Python process reads the value of PYTHONPATH at startup, so modifying that environment variable while the process is already running will not change where that process looks for packages. Python 进程在启动时读取PYTHONPATH的值,因此在进程已经运行时修改该环境变量不会改变该进程查找包的位置。 You should instead adjust the value of sys.path :您应该改为调整sys.path的值:

import sys
sys.path.extend(['/content/models/research/', '/content/models/research/slim/'])

The solution depends on your operation system, Linux or Windows.解决方案取决于您的操作系统,Linux 或 Windows。 Someone has already asked the same question: tutorialTensorflow object detection: ImportError: No module named nets.有人已经问过同样的问题:tutorialTensorflow object detection: ImportError: No module named nets。 Tensorflow object detection: ImportError: No module named nets If you use Windows, changing the PYTHONPATH may not work. Tensorflow 对象检测:ImportError:No module named nets如果您使用 Windows,更改 PYTHONPATH 可能不起作用。 Here is a try.这是一个尝试。 First, run the file setup.py .首先,运行文件setup.py

python setup.py build
python setup.py install

and it may give you a waring error: could not create 'build' (because the file has already existed).它可能会给你一个警告error: could not create 'build' (因为文件已经存在)。 Because there is a file named "build" in what you git clone.因为在您 git clone 的内容中有一个名为“build”的文件。 However, the command "build" and "install" need to make a new folder named "build" .但是,命令 "build" 和 "install" 需要创建一个名为 "build" 的新文件夹。 I do not know what the file "build" is used for, so I choose to move the file to another directory and use the command above, and it will work.我不知道文件“build”是干什么用的,所以我选择将文件移动到另一个目录并使用上面的命令,它会起作用。

%cd /content/models/research/slim
!python setup.py build
!python setup.py install
%cd /content/models/research/deeplab

doing this got me solved...这样做让我解决了...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM