pythontensorflowpython-venv

Downgrade Python version in virtual environment


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?


Solution

  • 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!