从setup.py安装时,无法在Google Colab中导入Tensorflow 2.2.0rc2



我试图在Google Colab中导入Tensorflow的最新rc2版本(目前为2.2.0rc2(,但从我的setup.py安装脚本安装时无法完成。

当我从Colab单元使用!pip install tensorflow==2.2.0rc2手动安装Tensorflow时,一切都很好,我可以导入Tensorflow。

下一个是如何在Google Colab:中安装依赖项

# Executes the cell in bash mode
%%bash
if [ ! -d "/content/deep-deblurring/" ]; 
then 
git clone https://github.com/ElPapi42/deep-deblurring;
cd deep-deblurring/
else 
cd deep-deblurring/; 
git pull; 
fi;
git checkout development
cd ..
pip uninstall -y tensorflow tensor2tensor tensorboard tensorboardcolab tensorflow-datasets tensorflow-estimator tensorflow-gan tensorflow-hub tensorflow-metadata tensorflow-privacy tensorflow-probability
pip install colab-env
pip install --upgrade grpcio
cd deep-deblurring/
python setup.py install
cd ..

下一个是我的setup.py文件:

#!/usr/bin/python
# coding=utf-8
"""Setup and install the package and all the dependencies."""
from setuptools import setup, find_packages
with open('requirements.txt') as pro:
INSTALL_REQUIRES = pro.read().split('n')
setup(
author='Whitman Bohorquez, Mo Rebaie',
author_email='whitman-2@hotmail.com',
name='deblurrer',
license='MIT',
description='Image Deblurring using Deep Learning Architecture',
version='1.0.0',
url='',
packages=find_packages(),
include_package_data=True,
python_requires='>=3.6',
install_requires=INSTALL_REQUIRES,
classifiers=[
'Development Status :: Alpha',
'Programming Language :: Python',
'Programming Language :: Python :: 3.6',
'Intended Audience :: Developers',
],
)

下一个是存储库上的requirements.txt:

grpcio == 1.27.2
kaggle
numpy
tensorflow >= 2.2.0rc2
pandas

事实上,Google Colab附带了Tensorflow 2.2.0rc1,但我想要rc2。当我执行时:

import tensorflow as tf
print(tf.__version__)

在执行setup.py安装脚本之前,导入工作正常。但是在使用setup.py完成安装之后,会抛出错误ImportError: No module named 'tensorflow'

我在执行python setup.py install之前和之后检查了tensorflow的安装,一切似乎都很好,安装之前是tensorflow 2.2.0rc1,安装之后是2.2.0rc2。

正如我首先提到的,当使用!pip install tensorflow==2.2.0rc2手动安装tensorflow时,导入会按规定工作,所以问题一定是围绕setup.py文件或需求,类似的东西,但我没有看到。

希望你们的帮助!

PD:这个项目设置在上周五开始工作,但今天我试着运行它,突然莫名其妙地停止了工作。

PD2:https://colab.research.google.com/drive/1Qv8h4ceEtDTq5lvt1uKJG8dk53_bUqBZ这是我和你分享的Colab笔记本,这设置了重现问题的代码。

PD3:这是在导入tensorflow时在Google Colab中进行的完整错误回溯抛出:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/usr/lib/python3.6/importlib/_bootstrap.py in _find_spec(name, path, target)
AttributeError: '_TensorflowImportHook' object has no attribute 'find_spec'
During handling of the above exception, another exception occurred:
ImportError                               Traceback (most recent call last)
2 frames
<ipython-input-7-69e5d056d1fc> in <module>()
----> 1 import tensorflow as tf
2 
3 tf.__version__
/usr/local/lib/python3.6/dist-packages/google/colab/_import_hooks/_tensorflow.py in find_module(self, fullname, path)
26     if fullname != 'tensorflow':
27       return None
---> 28     self.module_info = imp.find_module(fullname.split('.')[-1], path)
29     return self
30 
/usr/lib/python3.6/imp.py in find_module(name, path)
295         break  # Break out of outer loop when breaking out of inner loop.
296     else:
--> 297         raise ImportError(_ERR_MSG.format(name), name=name)
298 
299     encoding = None
ImportError: No module named 'tensorflow'
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------

tensorflow没有错,但Colab的_TensorflowImportHook缺少find_specimpl,因此如果将tensorflow安装为egg-dir,它将上升。由于钩子除了发出通知将tensorflow更新到2.0之外,没有任何有用的作用,并且计划无论如何都要删除,因此一个简单的修复方法是在笔记本电脑开头的某个位置将其从sys.meta_path清除:

[1] import sys
sys.meta_path[:] = [hook for hook in sys.meta_path if not h.__class__.__name__ == '_TensorflowImportHook']
[2] import tensorflow as tf
print(tf.__version__)

我找到了一个解决方案,但到目前为止,这还不是这个问题的解决方案,所以这不会被接受为解决方案,而是会帮助有同样困难的人继续他们的工作:

在安装自定义软件包之前手动安装您的需求,在我的情况下,这是pip install -r "/content/deep-deblurring/requirements.txt":

# Executes the cell in bash mode
%%bash
if [ ! -d "/content/deep-deblurring/" ]; 
then 
git clone https://github.com/ElPapi42/deep-deblurring;
cd deep-deblurring/
else 
cd deep-deblurring/; 
git pull; 
fi;
git checkout development
cd ..
pip uninstall -y tensorflow tensor2tensor tensorboard tensorboardcolab tensorflow-datasets tensorflow-estimator tensorflow-gan tensorflow-hub tensorflow-metadata tensorflow-privacy tensorflow-probability
pip install colab-env
pip install --upgrade grpcio
pip install -r "/content/deep-deblurring/requirements.txt"
cd deep-deblurring/
python setup.py install
cd ..

目前这修复了导入问题,但这不是一个干净的解决方案,希望稍后能有更好的解释!

最新更新