How to downgrade python version from 3.8 to 3.7 (mac) - python

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.

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

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

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

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

Related

Are there any dangers/concerns for downgrading python 3.8 to 3.7 (VM)

I've recently installed an Ubuntu VM, and it came with python 3.8. However this is version of python is incompatible with a lot of programs I want to use, so I was looking at downgrading it. But looking at different posts, it seems basic linux programs are dependent on the built in python, and removing/modifying it could break those programs. Thus:
Should I/can I keep 3.8 and just download 3.7 as well (and just use 3.7 for all my uses)
Can I delete 3.8 and reinstall 3.7 (will I need to reconfigure all the programs with it though? Since they will break if I delete 3.8)
Normally I would not change version of python, that a linux distribution is delivered with.
I personally use pyenv to compile and download other than the system's python versions and virtualenv to create one virtualenv per project.
Then you can use direnv or autoenv to automatically activate them if you cd in a projects directory.
Relevant links:
https://github.com/pyenv/pyenv
https://pypi.org/project/virtualenv/
https://github.com/direnv/direnv
To install pyenv something like:
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc
exec "$SHELL"
To download / install python3.7
pyenv install
3.7.7
To install virtualenv for this python version
~/.pyenv/versions/3.7.7/bin/python -m pip install -U pip virtualenv
To create a virtualenv for a projet
venv_path="~/venv/pjt1" # set the value where your project's venv should be stored
~/.pyenv/versions/3.7.7/bin/python -m virtualenv -p ~/.pyenv/versions/3.7.7/bin/python "$venv_path"
source $venv_path/bin/activate

How do I downgrade my version of python from 3.7.5 to 3.6.5 on ubuntu

So currently, I have ubuntu 19. And it comes by default with python 3.7.5. I need to downgrade to 3.6.5.
EDIT:
I am using virtualenv
The following talks about upgrade from 3.6.7 to 3.7.0 but you can use the same process for downgrade. You should not change the system python unless you really know what you're doing
First Install Pyenv
Installlation Instructions are here
Look at Pyenv Options
$ pyenv
pyenv 1.2.14
Usage: pyenv <command> [<args>]
Some useful pyenv commands are:
commands List all available pyenv commands
activate Activate virtual environment
commands List all available pyenv commands
deactivate Deactivate virtual environment
doctor Verify pyenv installation and deevlopment tools to build pythons.
exec Run an executable with the selected Python version
global Set or show the global Python version
help Display help for a command
hooks List hook scripts for a given pyenv command
init Configure the shell environment for pyenv
install Install a Python version using python-build
local Set or show the local application-specific Python version
prefix Display prefix for a Python version
rehash Rehash pyenv shims (run this after installing executables)
root Display the root directory where versions and shims are kept
shell Set or show the shell-specific Python version
shims List existing pyenv shims
uninstall Uninstall a specific Python version
--version Display the version of pyenv
version Show the current Python version and its origin
version-file Detect the file that sets the current pyenv version
version-name Show the current Python version
version-origin Explain how the current Python version is set
versions List all Python versions available to pyenv
virtualenv Create a Python virtualenv using the pyenv-virtualenv plugin
virtualenv-delete Uninstall a specific Python virtualenv
virtualenv-init Configure the shell environment for pyenv-virtualenv
virtualenv-prefix Display real_prefix for a Python virtualenv version
virtualenvs List all Python virtualenvs found in `$PYENV_ROOT/versions/*'.
whence List all Python versions that contain the given executable
which Display the full path to an executable
See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme
Look at Python Versions
$ pyenv versions
system
* 3.6.7 (set by /home/taarimalta/.pyenv/version)
Install a new Python
$ pyenv install 3.7.0
Installing Python-3.7.0...
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
WARNING: The Python sqlite3 extension was not compiled. Missing the SQLite3 lib?
Installed Python-3.7.0 to /home/taarimalta/.pyenv/versions/3.7.0
If you run into an issue with _ctypes install libffi-dev library
Now look at the versions
$ pyenv versions
system
* 3.6.7 (set by /home/taarimalta/.pyenv/version)
3.7.0
Select 3.7.0 for local environment
$ pyenv local 3.7.0
See that the version changed
$ pyenv versions
system
3.6.7
* 3.7.0 (set by /home/taarimalta/.python-version)
$ python
Python 3.7.0 (default, Jan 1 2020, 10:52:57)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Switch to a different folder
cd ../project2
pyenv versions
system
* 3.6.7 (set by /home/taarimalta/.pyenv/version)
3.7.0
The python version may be different here depending on which python version you have set locally
Set pyenv version globally
This globally sets a python version for a user
pyenv global 3.7.0
Note that pyenv sets local version by adding a .python-version file
$ pyenv local 3.7.0
$ cat .python-version
3.7.0
Note that pyenv knows the global version by looking at the ~/.pyenv/version file
cat ~/.pyenv/version
3.8.2
Install it from the pre-compiled Ubuntu deb package repos.
$ sudo apt update
$ sudo apt install software-properties-common
$ sudo add-apt-repository ppa:deadsnakes/ppa
When prompted, pres ENTER to continue.
sudo apt install python3.6
Alternatively, you can easily create a virtual environment.
Suppose you have python 3.8 (or higher) installed on the system, but for a specific task, you need python 3.7 (or lower). The best idea is to Create a virtual environment with python 3.7(or any 3.x, change the commands below according to your desired version. Below is an implementation of a virtual environment with python 3.7)
Steps: (Checked August 2022)
Install python 3.7 and it’s virtual environment packages.
sudo apt-get install python3.7-dev python3.7-venv
Find out where your python 3.7 is located by this command:
which python3.7 (Should be something like /usr/bin/python3.7, if not found, then install python 3.7 manually)
Create Virtual Environment in the Home directory.
cd
mkdir virtual_env
/usr/bin/python3.7 -m venv ~/virtual_env/venv_with_python3.7
source ~/virtual_env/venv_with_python3.7/bin/activate
python --version (Should be python 3.7 now)
Done. Python 3.7 can be used in this virtual environment. Type which python, and you’ll see you have created python 3.7 in a virtual environment, rather than in the system globally.
Run deactivate when you need to deactivate.
You can also use virtualenv to use different Python environments.
If you type python and tab twice or so, you might see a number of Python versions available. I say this because when I type
python3.6 -V
I have
Python 3.6.9
available. My alias for python is 3.7.5
When you type python you are probably referring to an alias defined in .bashrc

pipenv insists on installing Python 3.6

I set up my Python dev environment on macOS using the following commands:
brew install pyenv
pyenv install 3.7.0
pyenv install 2.7.15
pyenv global 3.7.0
I also added the following to my .bash_profile:
export PATH="/Users/me/.local/bin:$PATH"
eval "$(pyenv init -)"
Python seems to be working as expected:
01:29 $ python --version
Python 3.7.0
I then installed pipenv the "pragmatic" way, since installing via Homebrew would install a whole copy of Python as well (which I didn't want, since I was using pyenv already):
pip install --user pipenv
Now when I try to start a new pipenv project I get the following message:
01:28 $ mkdir my-new-project && cd my-new-project && pipenv install
Warning: Python 3.6 was not found on your system...
Would you like us to install CPython 3.6.6 with pyenv? [Y/n]:
I thought pipenv automatically used whatever pyenv copy of Python was available -- which is 3.7.0 in my case. Where is the prompt about installing CPython 3.6.6 coming from?
Short answer: if you're seeing weird behavior like this, run pipenv --support and read carefully.
I had a stray Pipfile that was hanging out in the parent directory of my project that was dictating the version of Python to install. I'm guessing there's some sort of recursive search during pipenv install that looks for a Pipfile in any directory outside the current one.

Homebrew: PATH env var is modified before installation of Formulae (mercurial will use system python instead of homebrew python)

Trying to use Homebrew on Mac OS X 10.9 to install mercurial and make sure it's using homebrew python version.
I've updated /etc/paths to list /usr/local/bin before /usr/bin.
Installed python 2.7.9 using brew install python, the python executable is located at /usr/local/bin/python which is a symlink to /usr/local/Cellar/python/2.7.9/bin/python.
When I type which python or python -V I'm getting the homebrew python version.
Later, when I run brew install mercurial the installed file /usr/local/bin/hg shebang (#!) points to the system python /usr/bin/python.
This is happening because mercurial python package installer (distutils) is using the PATH variable to hardcode the path to python in shebang instead of #!/usr/bin/env python.
When I check my PATH environment variable I see /usr/local/bin before /usr/bin, but if I try to install a brew package interactively: brew install -i mercurial I get a shell with a different PATH variable without /usr/local/bin at all.
How can I install mercurial to make sure it will use the homebrew version of python ?
I prefer a way that would survive mercurial package upgrades as well.
By default the formula only uses system Python. You can bypass Superenv (which sanitizes PATH) and it should pick up whatever is in PATH when building from source:
$ brew reinstall mercurial --build-from-source --env=std
$ head -n 1 /usr/local/bin/hg
#!/usr/local/opt/python/bin/python2.7

How can I use Homebrew to install both Python 2 and 3 on Mac?

I need to be able to switch back and forth between Python 2 and 3. How do I do that using Homebrew as I don't want to mess with path and get into trouble.
Right now I have 2.7 installed through Homebrew.
I would use pyenv You can install it:
$ brew install pyenv
To enable pyenv in your Bash shell, you need to run:
$ eval "$(pyenv init -)"
To do this automatically for Bash upon startup, add that line to your ~/.bash_profile. 1
Usage:
Once you have installed pyenv and activated it, you can install different versions of python and choose which one you can use. Example:
$ pyenv install 2.7.5
You can check the versions you have installed with:
$ pyenv versions
And you can switch between python versions with the command:
$ pyenv global 3.3.1
Also you can set a python version for the current directory with:
$ pyenv local 3.5.2
You can check by running python --version:
$ python --version
Python 3.5.2
1 Homebrew used to instruct you to do this upon installation of pyenv, but the message was removed. For Zsh and other shells, the precise steps may be different.
You can have both versions installed at the same time.
For Homebrew >=1.5.0:
Since 1st March 2018 the python formula will be upgraded to Python 3.x, while a new python#2 formula will be added for Python 2.7, specifically.
See changes announcement here or the final doc about using Homebrew for Python here.
For older Homebrew:
For Python 2.x:
brew install python
For Python 3.x:
brew install python3
Now, you will have both the versions installed in your machine. When you want to use version 2, use the python executable. When you want to use version 3, use the python3 executable.
Currently Homebrew provides two different formulas for Python 2 and 3. brew install python installs python3, and brew install python#2 installs python2. More details in Homebrew docs:
https://docs.brew.sh/Homebrew-and-Python
If you currently have 2.x installed via Homebrew, Homebrew will give you a message such as:
Error: python 2.7.14 is already installed
To upgrade to 3.6.5, run `brew upgrade python`
If you run:
brew upgrade python
you should be able to do:
python --version
and
python3 --version
To see what versions of Python 2.x and 3.x installed.
There are ways to use both , but the simplest solution today is to use pyenv. pyenv allows easy switching between versions.
Here is what I did to set up:
STEP1:
Remove all pythons from your mac
brew uninstall --ignore-dependencies --force python
sudo rm -rf ~/miniconda3/
sudo rm -rf ~/.conda/
Remove the following from ~/.bash_profile
export PATH="/Users/ishandutta2007/miniconda3/bin:$PATH"
and also the following from ~/.bashrc
export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
export PYTHONPATH=/usr/local/lib/python2.7/site-packages/google:$PYTHONPATH
alias python="/usr/bin/python"
STEP2:
Install pyenv and the python versions you need
brew update
brew install pyenv
pyenv install 2.7
pyenv install 3.7.0
STEP3:
add pyenv init to bash_profile or bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
STEP4:
Check what got installed
pyenv versions
system (set by /Users/ishandutta2007/.pyenv/version)
2.7
3.7.0
STEP5:
Choose a default
pyenv global 3.7.0
When a project needs older version, just go its root folder and run
pyenv local 2.7
Alternatively, you probably can just enter "python3" to run your most current version of python3.x and "python" or "python2" to run the latest installed 2.x version.
Use asdf !
Ballad of asdf
Once upon a time there was a programming language
There were many versions of it
So people wrote a version manager for it
To switch between versions for projects
Different, old, new.
Then there came more programming languages
So there came more version managers
And many commands for them
I installed a lot of them
I learnt a lot of commands
Then I said, just one more version manager
Which I will write instead
So, there came another version manager
asdf version manager - https://github.com/asdf-vm/asdf
A version manager so extendable
for which anyone can create a plugin
To support their favourite language
No more installing more version managers
Or learning more commands
https://github.com/asdf-vm/asdf
https://github.com/tuvistavie/asdf-python
https://github.com/asdf-vm/asdf-plugins
I thought I had the same requirement - to move between Python versions - but I achieved all I needed with only Python3.6 by building from source instead of using homebrew.
git clone https://git.<theThingYouWantToInstall>
Depending on the repo, check if there is MAKE file already setup for this option.
I was able to just go to https://www.python.org/downloads/mac-osx/ and download the latest python. It installed along side current python in my system.
Okay, I was struggling with my brew installation of Python3, because I didn't have pip3
sudo pip3 command not found
and so I did
brew uninstall --force --ignore-dependencies python3
and installed the regular Python 3.6.2 from official distribution and then I had pip3 and all components were ok.

Categories