[python] How to downgrade python from 3.7 to 3.6

I'm trying to install tensorflow but it needs a Python 3.6 installation and I only have Python 3.7 installed. I tried to switch using brew and pyenv but it doesn't work.

Does anyone know of a way to solve this problem?

This question is related to python python-3.x

The answer is


Here is a canonical summary which sums up different solutions for the variety of operating system Python runs on. What follows are possibilities for Microsoft Windows, Linux, macOS and Misc.

As mentioned those are just possibilities - by no means do I claim to have a complete list whatsoever.


Microsoft Windows

Option 1

In general, it's suggested to use virtual environments (I highly suggest looking at the official Python documentation). With this approach, you easily can set up project-specific Python versions (as well as libraries). Easily manageable and the best part: There are lots of tutorials on the internet on how to approach this:

1.) Open command prompt ("cmd") and enter pip install virtualenv.

2.) Install your desired Python version via https://www.python.org/downloads/release; Remember: Do not add to PATH!

3.) Type into the command prompt: virtualenv \path\to\env -p \path\to\python_install.exe, whereas \path\to\env shall be the path where your virtual environment is going to be and \path\to\python_install.exe the one where your freshly (presumably) installed Python version resides.

4.) Done! You now have a virtual environment set up! Now, to activate the virtual environment execute the batch file which is located inside the \path\to\env\Scripts\activate.bat. (cf. this website or an official Python guide)

Option 2

The basic option would be to uninstall the unwanted Python version and re-install the favored one from https://www.python.org/downloads/. To remove the "old" version go to Control Panel -> "Uninstall a program" -> Search for "Python" -> Right-click on the Python name -> Uninstall. Bear in mind that Python usually has a PATH variable stored, hence you should remove it as well - Check the following links for this:

Now double-check whether there are any remaining files where Python used to be stored. Usually, you can find all the Python files at either C:\Program Files (x86)\Pythonxx, C:\Users\username\AppData\Local\Programs\Pythonxx or C:\Pythonxx or all of them. You might have installed it in another directory - check where it once was.

Now after de-installing just re-install the wanted version by going to the download page and follow the usual installation process. I won't go into details on how to install Python.. Lastly, you might check which version is currently installed by opening the command prompt and typing python -V.

Option 3

This approach is pretty similar to the second one - you basically uninstall the old one and replace it by your favored version. The only thing that changes it the part regarding how to uninstall the unwanted Python distribution: Simply execute the Python3 installer you originally used to install Python (it's usually stored in your Python directory as mentioned above; for more assistance check out this). There you get an option to repair or uninstall, proceed by choosing uninstall, and follow the steps provided via the uninstaller.

No matter how you uninstall Python (there are many resources on this topic, for example this Stack Overflow question or a problem thread a user by the name of Vincent Tang posted on the Stack Exchange site Super User, etc.), just reinstall the wanted Python version by following the steps mentioned in Option 2.

Option 4

Option 4 deals with Anaconda. Please refer to this site on how to install Anaconda on Windows. Step 9 is important as you don't want to install it as your default Python - you want to run multiple versions of Python:

Choose whether to register Anaconda as your default Python. Unless you plan on installing and running multiple versions of Anaconda or multiple versions of Python, accept the default and leave this box checked.

Follow the official tutorial I linked above.

Once done you can create the following commands individually in the anaconda prompt: To overwrite the default python version system-wise use conda install python=3.6 or to create a virtual environment go ahead and use conda create -n $PYTHON36_ENV_NAME python=3.6 anaconda whereas $PYTHON36_ENV_NAME is the custom name you can set. Credit where credit is due - the user @CermakM from this thread strongly influenced this snippet.

In my research I encountered a bunch of useful Stack Overflow threads - you might check them out if you go the tough road with Anaconda:

Option 5

What follows isn't a downgrade in the classical sense - though for the sake of completeness I decided to mention this approach as well. On Windows you're also able to run multiple Python versions - an infamous thread on StackOverflow deals with this question, thus I politely refer you to there for further reading purposes.


Linux

Option 1

Pretty analog to the third option for Windows I highly suggest you use a virtual environment such as Anaconda. Anaconda - or short conda - is also available on Linux. Check the official installation documentation here. Once again this thread is highly suggested on how to overwrite a Python version, respectively how to specifically create an environment with your wanted Python version.

Option 2

Another highly suggested virtual environment is Pyenv. The user @Sawan Vaidya described in this Stack Overflow question on how to up-or downgrade a Python version with the help of Pyenv. You can either set a Python version globally or create a local environment - both explained in the mentioned thread.

Option 3

Another user, namely @Jeereddy, has suggested to use the software package management system Homebrew. He explained this option thoroughly in this current question:

$ brew unlink python
$ brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/e128fa1bce3377de32cbf11bd8e46f7334dfd7a6/Formula/python.rb
$ brew switch python 3.6.5

Option 5

No need to reinvent the wheel - this thread is filled with lots of beautiful running approaches such as the one by @Sidharth Taneja.

  1. Download your wanted Python version from https://www.python.org/downloads/release and install it as a normal package.
  2. Run cd /Library/Frameworks/Python.framework/Version
  3. Execute ls to list all installed Python versions
  4. Run sudo rm -rf 3.7, removing Python version 3.7 - can be repeated for whatever version(s) you want to delete
  5. Check python3 -v, it should display the version you originally wanted to have installed

Option 6

What a goldmine this thread is! As @nondetermistic has described in-depth (direct link to his post):

Install Python source code as it is like this:

#Taken Python 3.6 as an example
$ mkdir /home/<user>/python3.6
$ ./configure --prefix=/home/<user>/python3.6/
$ make altinstall

You're now able to either add the downloaded version (/home/<user>/python3.6/bin) to PATH as well as lib to LD_LIBRARY_PATH or just create a virtual environment by: /home/<user>/python3.6/bin/python3.6 -m venv env-python3.6. A very aesthetic and simple solution to run multiple Python versions on your system.


macOS

Option 1

Using pyenv with Homebrew - credit to @Shayan with his reply here:

1.) Installing pyenv with Homebrew:

brew update
brew install pyenv

2.) Cloning the GitHub repository to get latest pyenv version:

 git clone https://github.com/pyenv/pyenv.git ~/.pyenv

3.) Defining the environment variables as follows

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile

4.) Restarting shell so PATH change works

exec "$SHELL"

5.) Checking the available Python versions

pyenv install --list

6.) Installing the wanted Python version, e.g. 3.6

pyenv install 3.6

7.) Setting it globally (you can also go ahead and only use it in a certain environment)

pyenv global 3.6

8.) Check the Python version the system is using - your wanted / downgraded version should be displayed here.

python3 --version

Option 2

Similar to previous approaches you can download Anaconda on macOS as well. For an installation guide click here. The usage is pretty much the same as I've already described in Option 4 of the Windows guide. Please check out above.

Other options

In this case it's getting pretty repetitive. I kindly suggest you to check the following resources for further reading:


Misc

When writing this post I had the problem of not knowing where to draw the line. When looking up the operating systems Python currently supports you get a huge list, including the ones I mentioned, i.e. Linux, Microsoft Windows and macOS, though obviously different Linux distributions are single-handedly treated, e.g. CentOS, Arch Linux or Fedora should deserve a spot as well - or shall I make separate options for Windows 10, 7, etc.?

Due to the high degree of repetitiveness as far as modules like Homebrew, Conda or alike are concerned I decided to limit my list to the "main three" operating systems - distributions like Ubuntu (How do I downgrade my version of python from 3.7.5 to 3.6.5 on ubuntu), CentOS (How to downgrade python version on CentOS?) can be easily researched on Stack Overflow. Most often than not you can apply solutions from the Linux tab for said distributions. The same goes with Windows and macOS (versions).


create a virtual environment, install then switch to python 3.6.5

$ conda create -n tensorflow python=3.7
$ conda activate tensorflow
$ conda install python=3.6.5
$ pip install tensorflow

activate the environment when you would want to use tensorflow


I would just recommend creating a new virtual environment and installing all the packages from the start as the wheels for some packages might have been installed for the previous version of the Python. I believe this is the safest way and you have two options.

  1. Creating a new virtual environment with venv:

    python3.6 -m venv -n new_env
    source venv_env/bin/activate
    
  2. Creating a conda environment:

    conda create -n new_env python=3.6
    conda activate new_env
    

The packages you install in an environment are built based on the Python version of the environment and if you do not carefully modify the existing environment, then, you can cause some incompatibilities between packages. That is why I would recommend a using a new environment built with Python 3.6.


Download python 3.6.0 from https://www.python.org/downloads/release/python-360/

Install it as a normal package.

Run cd /Library/Frameworks/Python.framework/Version

Run ls command and all installed Python versions will be visible here.

Run sudo rm -rf 3.7

Check the version now by python3 -V and it will be 3.6 now.


If you use anaconda, you can just create a new environment with the specified version. In case you don't want to keep the existing version 3.7, you can just uninstall it and install it from here.


A clean way without having to uninstall a previous version or reverting to additional software like Anaconda or docker, etc. is to download the Python 3.6 source code and install it as follows:

$ mkdir /home/<user>/python3.6
$ ./configure --prefix=/home/<user>/python3.6/
$ make altinstall

To use it you either:

  • add /home/<user>/python3.6/bin to your PATH (and lib to LD_LIBRARY_PATH) and be done with it. (You may also need to add to your include path etc., depending on what you're trying to achieve exactly - but you get the idea, I hope.);

  • or, you create a virtual environment similar to this: /home/<user>/python3.6/bin/python3.6 -m venv env-python3.6.

No sudo or root access required. No messing up your system.


Download and install Python 3.6 and then change the system path environment variable to that of python 3.6 and delete the python 3.7 path system environment variable. Restart pc for results.


pyenv can be used in Linux/MacOS for python version management. pyenv-win is the fork of pyenv which can be used on Windows.

Installation

MacOS

Tested on Mac Catalina

  1. Install pyenv.

    brew install pyenv
    
  2. Add following to your shell config file:

    • .bashrc/.bash_profile - For Bash
    • .zshrc - For Zsh
    export PYENV_ROOT="$HOME/.pyenv"
    export PATH="$PYENV_ROOT/bin:$PATH"
    eval "$(pyenv init -)"
    
  3. Restart your shell. Start a new shell or run exec "$SHELL" in your current shell.

Linux / Windows on Linux Subsystem

Tested on Arch Linux

  1. Install pyenv on your system.

    curl https://pyenv.run | bash
    
  2. Follow same steps as in Step 2 and 3 of MacOS installation.

Windows

  1. Install pyenv-win on Windows.

    In Powershell

    pip install pyenv-win --target "$HOME\.pyenv"
    

    In cmd.exe

    pip install pyenv-win --target "%USERPROFILE%\.pyenv"
    
  2. Setup the environment variables using Powershell/Terminal.

    [System.Environment]::SetEnvironmentVariable('PYENV',$env:USERPROFILE + "\.pyenv\pyenv-win\","User")
    [System.Environment]::SetEnvironmentVariable('PYENV_HOME',$env:USERPROFILE + "\.pyenv\pyenv-win\","User")
    [System.Environment]::SetEnvironmentVariable('path', $HOME + "\.pyenv\pyenv-win\bin;" + $HOME + "\.pyenv\pyenv-win\shims;" + $env:Path,"User")
    
  3. Close and re-open your terminal. Run pyenv --version on the terminal.

    a. If the return value is the installed version of pyenv, then continue below. b. If you receive a command not found error, ensure the environment variables are properly set via the GUI: This PC ? Properties ? Advanced system settings ? Advanced ? Environment Variables... ? PATH c. If you receive a command not found error and you are using Visual Studio Code or another IDE with a built in terminal, restart it and try again.

  4. Run pyenv rehash from the home directory.

Usage

Check installed python versions

pyenv versions

Example

$ pyenv versions
* system (set by /home/souser/.pyenv/version)
  3.6.9

Installed a specific python version

pyenv install <version-number>

Uninstall an installed python version

pyenv uninstall <version-number>

Set a python version as system-wide python version

pyenv global <version-number> # <version-number> is the name assigned to your python in output of `pyenv versions`

Example

$ python --version
Python 3.9.1
$ pyenv global 3.6.9
$ python --version
Python 3.6.9
Set a python version for a directory and all it's sub-directories
pyenv local <version-number> # <version-number> is the name assigned to your python in output of `pyenv versions`

Example

~/tmp/temp$ python --version
Python 3.9.1
~/tmp/temp$ pyenv local 3.6.9
~/tmp/temp$ python --version
Python 3.6.9

For more details, you can check the Github repos : pyenv and pyenv-win.


I just now downgraded my Python 3.9 to 3.6 because I wanted to use the librosa package but it does not support Python 3.9 still now.

Steps -

  • Go to python official website
  • Download the Python version which you want
  • Install in your machine normally

Run python3 --version in the terminal and it will show this version of Python.


$ brew unlink python
$ brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/e128fa1bce3377de32cbf11bd8e46f7334dfd7a6/Formula/python.rb
$ brew switch python 3.6.5
$ pip install tensorflow

If you are working with Anaconda, then

conda install python=3.5.0
# or maybe 
conda install python=2.7.8
# or whatever you want....

might work.


I was having trouble installing tensorflow with python 3.7 and followed these instructions to have a virtual environment setup with python3.6 and got it working

Download the Python3.6 tgz file from the official website (eg. Python-3.6.6.tgz)
Unpack it with tar -xvzf Python-3.6.6.tgz
cd Python-3.6.6
run ./configure
run make altinstall to install it (install vs altinstall explanation here 

setting up python3.6 virtual environment for tensorflow

If you are using jupyter notebook or jupyter lab this can be helpful to choose the right virtual environment

python -m venv projectname
source projectname/bin/activate
pip install ipykernel
ipython kernel install --user --name=projectname

At this point, you can start jupyter, create a new notebook and select the kernel that lives inside your environment.

virtual environment and jupyter notebooks

Hope this helps


For those who want to add multiple Python version in their system: I easily add multiple interpreters by running the following commands:

  • sudo apt update
  • sudo apt install software-properties-common
  • sudo add-apt-repository ppa:deadsnakes/ppa
  • sudo apt install python 3.x.x
  • then while making your virtual environment choose the interpreter of your choice.

Create a python virtual environment using conda, and then install the tensorflow:

$ conda create -n [environment-name] python=3.6
# it may ask for installing python-3.6 if you don't have it already. Type "y" to proceed...
$ activate [environment-name]
$ pip install tensorflow

From now on, you can activate the environment whenever you want to use tensorflow.

If you don't have the conda package manager, first download it from here: https://www.anaconda.com/distribution