python - 从 setup.py 安装时,无法在 Google Colab 中导入 Tensorflow 2.2.0rc2
问题描述
我试图在 Google Colab 中导入最新的 rc2 版本的 Tensorflow(目前为 2.2.0rc2),但是从我的 setup.py 安装脚本安装时无法执行此操作。
当我使用!pip install tensorflow==2.2.0rc2
Colab 单元手动安装 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'
就会抛出错误。
我在执行之前和之后检查了 tensorflow 安装python setup.py install
,一切似乎都很好,安装前是 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 Notebook,这设置了重现问题的代码。
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_spec
impl ,因此如果tensorflow
安装为 egg 目录,它会提高。由于钩子除了发出更新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__)
推荐阅读
- java - 操作无法完成。找不到 Java 运行时。有关安装 Java 的信息,请访问 http://www.java.com
- apache-flink - 如何长时间保存 flink 日志?
- python - 在二维张量上滚动具有可变步长值的行
- java - 两个TextViews在Android中的setOnClickListener
- javascript - 如何过滤 JqGrid 以便仅在特定列上显示负值?
- libraries - 如何在 Windows 10 上安装 libportaudio
- pandas - 增量添加 pandas 列值
- docker - 无法通过 tcp://localhost:2375 连接到 Docker 守护程序。docker 守护进程是否正在运行?Gitlab CI/CD
- xamarin.forms - 删除 Xamarin Forms AppShell 标题栏(工具栏)上的填充/边距
- printing - 如何在应用程序服务器上将假脱机请求转换为 PDF 文件?