Title: Jupyter notebook in vscode with virtual environment fails to import tensorflow
Tags: python;visual-studio-code;jupyter-notebook;tensorflow2.0
Question: I'm attempting to create an isolated virtual environment running tensorflow &amp; tf2onnx using a jupyter notebook in vscode.
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.
The following is my setup procedure:
python -m venv .venv
Then after starting a new terminal in vscode:
pip install tensorflow==2.7.0
pip freeze &gt; requirements.txt
After this, in a cell in my jupyter notebook, the following line fails
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 &gt;= 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.  I've attempted to pre-install the 3.20.1 version into the virtual environment before installing tensorflow but this yields no effect.
Here is the full requirements file after installing tensorflow:
absl-py==1.0.0
astunparse==1.6.3
cachetools==5.1.0
certifi==2151.154.11.224
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
Comment: First rule of troubleshooting: remove confounding factors. Why are VS Code and Jupyter relevant to this question? Does this work without them?

Comment: Try this https://stackoverflow.com/a/72493690/6194097 for a proper solution.

Here is the accepted answer: A recent change in protobuf is causing TensorFlow to break. Downgrading before installing TensorFlow might not work because TensorFlow might be bumping up the version itself. Check if that is what happens during the installation.
You might want to either:
Downgrade with
pip install --upgrade &quot;protobuf<=3.20.1&quot;
after installing TensorFlow, or
Upgrade TensorFlow to the latest version, as TensorFlow has updated their setup file in their 2.9.1 release.
Comment for this answer: Downgrading to protobuf 3.20.1 as suggested here fixed the problem with tf 2.7.0
Comment for this answer: This works for me (TF 2.3.0)
Comment for this answer: For anyone who is looking for a proper solution,
answer:- [https://stackoverflow.com/a/72493690/6194097](https://stackoverflow.com/a/72493690/6194097)
Comment for this answer: @pceccon Do you get the same exception?
Comment for this answer: Didn't work for me with tensorflow==2.8.0


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2022:00:05/27/22', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><visual-studio-code><jupyter-notebook><tensorflow2.0>', 'Title': 'Jupyter notebook in vscode with virtual environment fails to import tensorflow', 'ViewCount': 3732, 'id': 72411825, 'user_AboutMe': None, 'user_AccountId': 23762596.0, 'user_DisplayName': 'RTD', 'user_Reputation': 103.0, 'user_Views': 19.0, 'user_id': 17777770.0}}