How to default Python3.8 on my Mac using Homebrew? - python-3.x

I have updated my python 3 to the latest version 3.8:
brew search python
==> Formulae
app-engine-python gst-python python ✔ python#3.8 ✔
boost-python ipython python-markdown wxpython
boost-python3 micropython python-yq
==> Casks
awips-python kk7ds-python-runtime mysql-connector-python
But when I check the python3 version on my mac it still shows 3.7:
python3 --version
Python 3.7.6
how can I default python3 to the latest 3.8 version using Homebrew ?
Edit:
When I tried to use brew switch, it tells me I only installed python 3.7.6, but with last brew upgrade I'm pretty sure that python3.8.1 is installed with Homebrew
brew switch python 3.8.1
python does not have a version "3.8.1" in the Cellar.
python's installed versions: 3.7.6_1

Here is the solution:
If existing symlinks belong to python 3.7 you should unlink them:
brew unlink python
Basically all you need to do:
brew link --force python#3.8
OR force the link and overwrite all conflicting files:
brew link --force --overwrite python#3.8
OR if needed list all files that would be deleted:
brew link --overwrite --dry-run python#3.8
Thus you can switch to any python version available in the Homebrew repo.
Also check out this answer for pyenv usage

Ok, thanks to #gromgit from Homebrew community discussion (https://discourse.brew.sh/t/how-to-default-python-3-8-on-my-mac-using-homebrew/7050)
Here is the solution:
$ brew info python#3.8
python#3.8: stable 3.8.1 (bottled) [keg-only]
...
==> Caveats
Python has been installed as
/usr/local/opt/python#3.8/bin/python3
...
python#3.8 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
If you need to have python#3.8 first in your PATH run:
echo 'export PATH="/usr/local/opt/python#3.8/bin:$PATH"' >> ~/.bash_profile
For compilers to find python#3.8 you may need to set:
export LDFLAGS="-L/usr/local/opt/python#3.8/lib"
For pkg-config to find python#3.8 you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/python#3.8/lib/pkgconfig"
I will stick to python (v3.7.6) at this time and wait for seamless upgrade of v3.8.1 in the future releases.

You might have to add python 3.8.1 to your PATH in your ~/.bash_profile and put it first so that it overrides previous installations. First find out where python 3.8.1 was installed, then add it to your path like this:
export PATH="/PATH_TO_PYTHON/:${PATH}"

I have a company Mac with Python 2.7 preinstalled to run older software.
$ brew install pyenv (successful)
$ pyenv install 3.9.2 (successful)
$ python --version
Python 2.7
$ pyenv global 3.9.2
$ python --version
Python 2.7
It still says Python 2.7, so, I did the following commands
pyenv init
eval "$(pyenv init -)"
$ python --version
Python 3.9.2
More details:
Link

Related

osx python not found but python3 found

I am unable to find or locate python in osx. but python3 works. however my pip packages are all installed not sure how with pip and not pip3. Any idea how I can use my pip packages with python3 and not having to installed them again with pip3? I believe python3 was installed via brew
% brew list
==> Formulae
autoconf kubernetes-cli mpdecimal python#3.10 virtualenv
ca-certificates libidn2 openssl#1.1 readline wget
gdbm libunistring pkg-config six xz
gettext m4 pyenv sqlite
==> Casks
mysqlworkbench virtualbox
vagrant visual-studio-code
You can use which python3 to locate your python installation
If your packages were installed for a different version of Python, they are very likely not compatible with your current version of Python, especially if you were using Python 2 before and now using Python 3. If you want to try to keep all your current packages, use pip freeze to export your current list of installed packages, remove the version numbers, and reinstall them using the pip of the Python distribution of your choice.

While uninstalling python 3.9 using brew uninstall python - Error: No available formula or cask with the name "python". is popping up

I had installed python3 using brew. This being my first time using terminal and using brew for that matter, I am not sure where I went wrong. after installing as suggested in another website, I used Pyenv to update the version of python and make the latest version the global one. but everytime I entered Python in terminal It showed Python 2.7
% python
WARNING: Python 2.7 is not recommended.
This version is included in macOS for compatibility with legacy software.
Future versions of macOS will not include Python 2.7.
Instead, it is recommended that you transition to using 'python3' from within Terminal.
Python 2.7.16 (default, Jun 18 2021, 03:23:52)
[GCC Apple LLVM 12.0.5 (clang-1205.0.19.59.6) [+internal-os, ptrauth-isa=deploy on darwin
Type "help", "copyright", "credits" or "license" for more information.
Brew info gives the below.
% brew info python3
python#3.9: stable 3.9.7 (bottled)
Interpreted, interactive, object-oriented programming language
https://www.python.org/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/python#3.9.rb
License: Python-2.0
==> Dependencies
Build: pkg-config ✔
Required: gdbm ✘, mpdecimal ✘, openssl#1.1 ✔, readline ✔, sqlite ✘, xz ✘
==> Caveats
Python has been installed as
/opt/homebrew/bin/python3
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/opt/homebrew/opt/python#3.9/libexec/bin
You can install Python packages with
pip3 install <package>
They will install into the site-package directory
/opt/homebrew/lib/python3.9/site-packages
tkinter is no longer included with this formula, but it is available separately:
brew install python-tk#3.9
See: https://docs.brew.sh/Homebrew-and-Python
==> Analytics
install: 448,214 (30 days), 1,500,839 (90 days), 7,290,725 (365 days)
install-on-request: 157,960 (30 days), 522,744 (90 days), 2,279,934 (365 days)
build-error: 0 (30 days)'
The not installed part made me confused but when I used Pyenv versions and it shows below.
% pyenv versions
system
* 3.9.7 (set by /Users/ronabraham/.pyenv/version)`
I also pasted the below into my .zshrc file.
export PYENV_ROOT="$HOME/.pyenv export PATH="$PYENV_ROOT/bin:$PATH if command -v pyenv 1>/dev/null 2>&1; then eval "$(pyenv init -)" fi
Even after this, Python command was still returning Python 2.7.
I again installed Python 3.9.7 from the official python downloads link. And tried uninstalling the Brew Python results as below.
% brew uninstall python3
Error: No available formula or cask with the name "python3". Did you mean python#3.7, python#3.9, python#3.8, ipython, bpython, jython or cython?`
brew uninstall python#3.9
Error: No available formula or cask with the name "python#3.9". Did you mean python#3.9, python-tk#3.9, python#3.8, python#3.7 or python-yq?
Will these 2 Pythons (brew python 3.9.7 and pkg installed 3.9.7 cause issues?) how can I remove the brew version or fix it and make it my default.
From what I can see, you probably missed closing " in your pyenv config setup.
This is mine for setting up pyenv in my local (I am using bash, so this is what I extracted from ~/.bash_profile):
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
After the pyenv setup, you can open a new terminal session to use the pyenv version of python.
Also, you don't have to uninstall brew versions of python, as it is needed for brew formulae (the python CLI apps).

Pip install on Mac OS gets error: command '/usr/bin/clang' failed with exit code 1

I want to install google-cloud-pubsub via pip installation on Mac OS but I get an error: distutils.errors.CompileError: command '/usr/bin/clang' failed with exit code 1. The command I run: pip install google-cloud-pubsub==2.1.0.
Here the complete error message.
Any suggestion? Thank you!
More info:
$ python -V
Python 3.9.0
$ pip -V
pip 20.2.4 from /.../lib/python3.9/site-packages/pip (python 3.9)
$ sw_vers
ProductName: macOS
ProductVersion: 11.0.1
BuildVersion: 20B29
I've seen other similar cases but them don't solve my issue. I tried:
Pip install error in Mac OS(error: command '/usr/bin/clang' failed with exit status 1)
Mac OS Mojave installation error - error: command 'clang' failed with exit status 1
Try to add these env var before
GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=true GRPC_PYTHON_BUILD_SYSTEM_ZLIB=true pip install google-cloud-pubsub==2.1.0
If it does not work you can try with virtualenv:
pip install virtualenv
virtualenv my-test-env
source my-test-env/bin/activate
my-test-env/bin/pip install google-cloud-pubsub==2.1.0
Because is written on github:
Install this library in a virtualenv using pip. virtualenv is a tool to create isolated Python environments. The basic problem it addresses is one of dependencies and versions, and indirectly permissions.
With virtualenv, it's possible to install this library without needing system install permissions, and without clashing with the installed system dependencies.
I was using pyenv and facing the similar kind of issue. Then I did the following and it worked.
First, upgrade pip
pip3 install --upgrade pip
Then, update the setup tools:
python3 -m pip install --upgrade setuptools
I just had this same problem but I'm using homebrew to manage my Mac packages.
My error result was the same as yours but within the error message it was:
plyvel/_plyvel.cpp:632:10: fatal error: 'leveldb/db.h' file not found
#include "leveldb/db.h"
This can be fixed using homebrew by just installing leveldb:
brew install leveldb
This allowed the plyvel dependency to be satisified. I then manually installed plyvel just to be safe:
pip3 install plyvel
And lastly because I'm trying to install the airflow libraries that include pubsub:
pip3 install apache-airflow-providers-google
I ended up just using Python 3.8.13 instead, managed with pyenv and pyenv-virtualenv.
If you don't have pyenv:
brew install pyenv pyenv-virtualenv
add
export PATH="$HOME/.pyenv/bin:$PATH"
export PATH="$HOME/.pyenv/shims:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
to your ~/.zshrc if it's not there, and source ~/.zshrc
pyenv install 3.8.13
pyenv global 3.8.13
pyenv virtualenv 3.8.13 venv
pyenv activate venv
pip install grcpio
Are you attempting to compile on a new M1 processor? I ran into trouble installing the latest grpcio (1.34.0) too, with the same error message as you.
Without messing with compile flags and libraries, I found the best solution for me (setting up a new Flask environment to talk to Google Cloud on a new Big Sur Mac) was the tip offered here:
https://osxdaily.com/2020/11/18/how-run-homebrew-x86-terminal-apple-silicon-mac/
Duplicate your Terminal rename it to "Rosetta Terminal" or similar
Get Info on the new app, and check the box for "Open using Rosetta"
Do your work with this new terminal app instead
grpcio installed fine for me this way, and I don't anticipate any problems running my Web apps in this translated environment (until I presume this issue gets fixed in the source libraries).
I encountered a similarly-named clang issue when attempting to awsiotsdk on an M1 Mac with Python 3.8
Incase it's relevant to anyone reading this, the underlying issue was a dependency on awscrt. This particular library (on PyPi) did not contain Built Distributions for the combination of M1 ("xxx_universal2.whl") and Python 3.8.
The solution was to use pyenv with pipenv (a favourite pattern of mine) to force this project to use a supported combination (in our case: Python 3.9 with M1 )
if you are using Mac M1 chip, just do the below thing it worked for me.
export GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=1
export GRPC_PYTHON_BUILD_SYSTEM_ZLIB=1
Thanks for the below answer.
How can I install GRPCIO on an Apple M1 Silicon laptop?
Working solution in my case (Mac M1 Monterey OS):
brew install openssl re2
LDFLAGS="-L$(/opt/homebrew/bin/brew --prefix openssl)/lib -L$(/opt/homebrew/bin/brew --prefix re2)/lib" CPPFLAGS="-I$(/opt/homebrew/bin/brew --prefix openssl)/include -I$(/opt/homebrew/bin/brew --prefix re2)/include" GRPC_BUILD_WITH_BORING_SSL_ASM="" GRPC_PYTHON_BUILD_SYSTEM_RE2=true GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=true GRPC_PYTHON_BUILD_SYSTEM_ZLIB=true pip install grpcio
Credits to https://github.com/grpc/grpc/issues/24677#issuecomment-862413344
I started having that issue. Following this comment,
with pyenv, I uninstalled my python 3.9.12 and reinstalled it again.
I am having an M1 and macOS 12.6, btw.
Deactivate your current virtualenv first, then:
pyenv uninstall 3.9.12
pyenv install 3.9.12
pyenv local 3.9.12 # to set your local python version to the newly installed python
poetry shell
pip install grpcio # or poetry install grpcio

Can't update Python from 3.6 to 3.7 in MacOS

I have tried several commands to update python 3.6 to python 3.7 in homebrew on MacOS.
I have tried (as administrator user):
brew update
This claimed to install python 3.7
brew upgrade
brew link python3
brew link --overwrite python3
brew unlink python && brew link python
brew switch python 3.7.0
brew switch python 3.7.5
Cleaning /usr/local/Cellar/python/3.7.5
24 links created for /usr/local/Cellar/python/3.7.5
After all those attempts, I still get this:
python3 --version
Python 3.6.5
Can someone please help me to get switched over to python3?
Based on the comment:
which python3 -> /Library/Frameworks/Python.framework/Versions/3.6/bin/python3
Your python3 is not the same one installed/managed by Homebrew.
(Maybe it's from a Python .pkg installer for Mac?).
First, install it via Homebrew:
$ brew uninstall python3 # let's start from scratch
$ brew install python3
Check where it's installed:
$ brew info python3
python: stable 3.7.5 (bottled), HEAD
...
==> Caveats
Python has been installed as
/usr/local/bin/python3
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/usr/local/opt/python/libexec/bin
If you need Homebrew's Python 2.7 run
brew install python#2
You can install Python packages with
pip3 install <package>
They will install into the site-package directory
/usr/local/lib/python3.7/site-packages
...
Notice that Homebrew installed it at /usr/local/bin/python3 and the site-packages are stored at the corresponding /usr/local/lib/python3.7/site-packages.
Next, you need to make sure your OS looks for python3 at that same path.
$ echo $PATH
/usr/local/sbin:/usr/local/opt/openssl/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
If you don't see /usr/local/bin there, add it to your PATH by adding this to your ~/.bash_profile:
export PATH=/usr/local/bin:$PATH
Then source the updated ~/.bash_profile (or restart your Terminal).
$ source ~/.bash_profile
$ echo $PATH
From the comment, if your PATH shows /Library/Frameworks/Python.framework/Versions/3.6/bin/python3, you'll either have to remove it by explicitly setting the full PATH in your .bash_profile or make sure it comes after Homebrew's Python in /usr/local/bin.
Finally, check that python3 is now correct:
$ which python3
/usr/local/bin/python
$ ls -l /usr/local/bin/python
lrwxr-xr-x 1 gino admin 38 Oct 4 17:35 /usr/local/bin/python3 -> ../Cellar/python/3.7.5/bin/python3
$ python3 -V
Python3.7.5
Notice that python3 should be the python3 installed by Homebrew in the ../Cellar directory.
Can I easily change to the homebrew installation, or will I lose all my installed packages?
I would recommend re-installing the packages over at Homebrew's python3's site-packages folder. If you maintained a requirements.txt file for your Python projects, it's as simple as:
$ python3 -m pip install -r requirements.txt
The final solution was that Python 3.7 was already installed and could be accessed using the command python3.7.

How to switch python version from 3.6.0 to 3.6.7 on Ubuntu 18.04?

I would am currently using python version 3.6.0 on Ubuntu 18.04 and need to use python version >= 3.6.1 to use PySlice_Unpack.
I have tried installing using the following commands:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6
The output from these commands is:
python3.6 is already the newest version (3.6.7-1~18.04).
So it seems like I already have python 3.6.7 installed. However when I run the commands python -V, python3 -V, or python3.6 -V the output is
Python 3.6.0 :: Continuum Analytics, Inc.
In /usr/bin/ I have config files for python2.7, python3.6, python3.6m, python3, and python3m. I've looked at the python3.6 and python3 config files and they both have VERSION="3.6". How can I switch to using python version 3.6.7 instead of 3.6.0?
If you need a different version of Python (or other versions of python packages than those included in the version of Ubuntu you are using), then you might want to try using VirtualEnv, like explained in this answer: https://stackoverflow.com/a/5507373/483566

Resources