Downgrade Python version in virtual environment
Asked Answered
G

3

12

I am always getting the same error regarding TensorFlow: ModuleNotFoundError: No module named 'tensorflow.contrib'.

I am actually using Python version 3.9 but, reading online, it seems that version 3.7 is the last stable one that can work with TensorFlow version >2.0.

Unfortunately I have started my project in a venv with the wrong version of Python and I would like to downgrade it, how can I do that?

Gopak answered 11/4, 2022 at 16:41 Comment(4)
Creating a new virtual environment is not particularly expensive; I would just start freshArchiplasm
You are right, my problem is that it took me weeks of work to reach my actual point. If there is another option instead of starting a new environment I would go for it.Guidotti
pip freeze > requirements.txt. When you recreate the new virtual environment, you can then install the same versions of whatever packages were in the original.Archiplasm
@Archiplasm tanks, I try then.Guidotti
E
4

Building on @chepner's comment above, since venvs are just directories, you can save your current state and start a fresh virtual environment instead.

# Save current installs
(venv) -> pip freeze -r > requirements.txt

# Shutdown current env
(venv) -> deactivate

# Copy it to keep a backup
-> mv venv venv-3.9

# Ensure you have python3.7
-> python3.7 -V

# Create and activate a 3.7 venv
-> python3.7 -m venv venv-3.7
-> source venv-3.7/bin/activate

# Reinstall previous requirements
(venv-3.7) -> pip install -r requirements.txt

# Install new requirements

Hope that helps!

Eraser answered 12/3, 2023 at 0:52 Comment(0)
B
1

There's the venv --upgrade <ENV_DIR> command, but it's mainly intended to update between patch versions (e.g. from 3.9.15 to 3.9.16).

You'll need extra steps to update between different minor versions. Let me walk you through it.

Assuming you have both versions of Python installed:

$ python3.9 --version
Python 3.9.16
$ python3.7 --version
Python 3.7.16

And that your environment was created using version 3.9, and it's in a venv subdirectory:

$ . venv/bin/activate
$ python --version
Python 3.9.16
$ deactivate

Use these commands to downgrade from 3.9 to 3.7:

$ python3.7 -m venv --upgrade venv
$ cd venv/bin
$ ln -sf python3.7 python
$ ln -sf python3.7 python3
$ rm {python,pip}3.9
$ cd -

Here's the result:

$ . venv/bin/activate
$ python --version
Python 3.7.16
Baseman answered 8/1, 2023 at 13:57 Comment(0)
C
-1

tf.contrib is deprecated from the latest version of TensorFlow 2.x and is replaced with TF Slim.

It is recommended to use the upgraded version of TensorFlow to have the benefit of the latest features and functionalities provided by TensorFlow.

However, To downgrade python

  1. You need to uninstall the existing python version and re-install the required python version and set up your environment.

  2. If you are using Anaconda IDE, then use the below command:

conda search python  #to check available python version
conda install python=<version>
    
conda create --name <env_name> python=<python_version> # Which also creates virtual environment
activate <env_name>

Please check this link for more details.

Cannes answered 5/5, 2022 at 18:5 Comment(1)
At the same time I have to mention that the previous user were right, creating a new virtual environment is not particularly expensive. The limit was my limited knowledge of the subject.Guidotti

© 2022 - 2024 — McMap. All rights reserved.