简体   繁体   English

具有虚拟环境的 vscode 中的 Jupyter 笔记本无法导入 tensorflow

[英]Jupyter notebook in vscode with virtual environment fails to import tensorflow

I'm attempting to create an isolated virtual environment running tensorflow & tf2onnx using a jupyter notebook in vscode.我正在尝试使用 vscode 中的 jupyter notebook 创建一个运行 tensorflow 和 tf2onnx 的隔离虚拟环境。

The tf2onnx packge recommends python 3.7, and my local 3.7.9 version usually works well with tensorflow projects, so I have local and global versions set to 3.7.9 using pyenv. tf2onnx 包推荐 python 3.7,我本地的 3.7.9 版本通常适用于 tensorflow 项目,所以我使用 pyenv 将本地和全局版本设置为 3.7.9。

The following is my setup procedure:以下是我的设置过程:

python -m venv .venv

Then after starting a new terminal in vscode:然后在 vscode 中启动一个新终端后:

pip install tensorflow==2.7.0

pip freeze > requirements.txt

After this, in a cell in my jupyter notebook, the following line fails在此之后,在我的 jupyter 笔记本的一个单元格中,以下行失败

import tensorflow.keras as keras

Exception:例外:

 TypeError: Descriptors cannot not be created directly. If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0. If you cannot immediately regenerate your protos, some other possible workarounds are: 1. Downgrade the protobuf package to 3.20.x or lower. 2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).

At this point, the protobuf package version is showing as v4.21.0 in my requirements file.此时, protobuf包版本在我的需求文件中显示为 v4.21.0。 I've attempted to pre-install the 3.20.1 version into the virtual environment before installing tensorflow but this yields no effect.在安装 tensorflow 之前,我尝试将 3.20.1 版本预安装到虚拟环境中,但这没有任何效果。

Here is the full requirements file after installing tensorflow:这是安装 tensorflow 后的完整需求文件:

absl-py==1.0.0
astunparse==1.6.3
cachetools==5.1.0
certifi==2022.5.18.1
charset-normalizer==2.0.12
flatbuffers==2.0
gast==0.4.0
google-auth==2.6.6
google-auth-oauthlib==0.4.6
google-pasta==0.2.0
grpcio==1.46.3
h5py==3.7.0
idna==3.3
importlib-metadata==4.11.4
keras==2.7.0
Keras-Preprocessing==1.1.2
libclang==14.0.1
Markdown==3.3.7
numpy==1.21.6
oauthlib==3.2.0
opt-einsum==3.3.0
protobuf==4.21.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
requests==2.27.1
requests-oauthlib==1.3.1
rsa==4.8
six==1.16.0
tensorboard==2.9.0
tensorboard-data-server==0.6.1
tensorboard-plugin-wit==1.8.1
tensorflow==2.7.0
tensorflow-estimator==2.7.0
tensorflow-io-gcs-filesystem==0.26.0
termcolor==1.1.0
typing-extensions==4.2.0
urllib3==1.26.9
Werkzeug==2.1.2
wrapt==1.14.1
zipp==3.8.0

A recent change in protobuf is causing TensorFlow to break . protobuf 的最新变化导致TensorFlow 崩溃 Downgrading before installing TensorFlow might not work because TensorFlow might be bumping up the version itself.在安装 TensorFlow 之前降级可能不起作用,因为 TensorFlow 本身可能会升级版本。 Check if that is what happens during the installation.检查这是否是安装过程中发生的情况。

You might want to either:您可能想要:

Downgrade with降级与

pip install --upgrade "protobuf<=3.20.1"

after installing TensorFlow, or安装 TensorFlow 后,或

Upgrade TensorFlow to the latest version, as TensorFlow has updated their setup file in their 2.9.1 release.将 TensorFlow 升级到最新版本,因为 TensorFlow 在 2.9.1 版本中更新了他们的设置文件。

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

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