46

I'm using Python & okta-aws tools and in order to fetch correct credentials on aws I need to run okta-aws init. But got an error message of Could not read roles from Okta and the system prompted that"Your Pipfile requires python_version 3.7, but you are using 3.8.3 (/usr/local/Cellar/o/1.1.4/l/.venv/bin/python).

I've tried to search all the Pipfiles on the mac and it seems that the Pipflie under my ~/Pipfile and /usr/local/Cellar/python@3.8/3.8.3_2/libexec/bin/Pipfile all have the same python version of 3.8, while the Pipfile under my /usr/local/Cellar/okta-aws-tools/1.1.4/libexec/Pipfile has required python_version = 3.7.

I've been struggling with this for a while and really not sure how I can fix this.

Nathan
  • 4,009
  • 2
  • 20
  • 21
user13902742
  • 505
  • 1
  • 4
  • 6

4 Answers4

155

Consider installing pyenv with Homebrew on macOS

brew update
brew install pyenv

OR Clone the repository to get the latest version of pyenv

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

Define your environment variables (For a recent MacOS you may want to replace ~/.bash_profile with ~/.zshrc as that is the default shell)

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
source ~/.bash_profile

Restart your shell so the path changes take effect

exec "$SHELL"

Verify the installation and check the available python versions

pyenv install --list

Install the required python version

pyenv install 3.7

Set it as your global version after installation

pyenv global 3.7

eval pyenv path

eval "$(pyenv init --path)"

Verify your current python version the system is using

python3 --version
Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88
Shayan
  • 1,931
  • 1
  • 9
  • 14
16

I recommend you to install and use pyenv, a Python Version Management. Once intalled pyenv, install python 3.7:

pyenv install 3.7

And then set the environment PYENV_VERSION to version of python you want to use, on this case will be 3.7:

pyenv shell 3.7
Nicolas Acosta
  • 742
  • 5
  • 12
4

brew only approach.

rm -rf $(brew --repository)/Library/Taps/company
brew tap-new company/team
brew extract python@3.7 company/team  --version=3.7.9 
HOMEBREW_NO_AUTO_UPDATE=1  brew install company/team/python@3.7.9
brew link --force company/team/python@3.7.9

This creates a local tap, extracts python 3.7.X to a formula in that local tap and then installs and links that formula

The created local tap and the new formula file can be found in $(brew --repository)/Library/Taps/company/homebrew-team

Hakan Baba
  • 1,897
  • 4
  • 21
  • 37
-1
  • Reinstalled xcode-select (used solution here)
  • Downgraded python 3.9 to 3.8.12 (used @Shayan's solution here)
  • Restarted terminal and checked default Python version, that's all

This is my solution for M1 Mac

  • 1
    How exactly did you "*Downgraded python 3.9 to 3.8.12*" and set it "*as global*"? Because that was the exact question, the OP was struggling to downgrade the Python used in the project from 3.8 to 3.7. So a more detailed set of steps would be useful. – Gino Mempin Nov 03 '21 at 01:25
  • While this code may answer the question, providing additional context regarding *why* and/or *how* this code answers the question improves its long-term value. – PCM Nov 03 '21 at 03:39
  • Did you need to reinstall `xcode-select` in order for @Shayan's solution to work? If so, any idea why? I'm on an M1 Mac and the default Python version isn't updated even though both `pyenv`'s `global` and `shell` versions are set correctly after restarting the terminal. – therightstuff May 04 '22 at 07:20
  • I don't see how reinstalling xcode-select is relevant (I'm using an M1 Mac and it was already installed), but the only addition I have to @Shayan's solution to make it work is to follow https://stackoverflow.com/a/69378384/2860309 and add `eval "$(pyenv init --path)"` into the `.zshrc` file. – therightstuff May 04 '22 at 07:28