Switching Python version installed by Homebrew - python

I have Python 3.8 and 3.9 installed via Homebrew:
~ brew list | grep python
python#3.8
python#3.9
I want to use Python 3.9 as my default one with python3 command. I tried the following:
~ brew switch python 3.9
Error: python does not have a version "3.9" in the Cellar.
python's installed versions: 3.8.6
I tried to uninstall Python and reinstall it, but it's used by other packages:
~ brew uninstall python
Error: Refusing to uninstall /usr/local/Cellar/python#3.8/3.8.6
because it is required by glib and php, which are currently installed.
You can override this and force removal with:
brew uninstall --ignore-dependencies python
How can I use Python 3.9?

There is an Homebrew known issue related to side by side install of Python 3.8 / 3.9. To workaround, following commands should work for you:
brew unlink python#3.9
brew unlink python#3.8
brew link --force python#3.9
Re-opening your terminal or execute command rehash can be required to take account the change.

Use pyenv. It's a software that lets you switch between any and all Python versions installed on your system. To install pyenv, use the following code in the command-line:
curl https://pyenv.run | bash
exec $SHELL
Then find the name of the python version you want to switch to with this:
pyenv versions
And select it with this:
pyenv global <version-name>
In your case, it's most likely named 3.9.0.

Updated MacOs Monterrey
For who are facing this problem add the pyenv path to your ~/.zshrc shell file.
export PATH="/Users/username/.pyenv/shims:${PATH}
eval "$(pyenv init --path)
Run in the terminal:
source ~/.zshrc
Check it out:
python3 --version
Font from the issue on GitHub.

Related

env: python3.9: No such file or directory

I have some python code formatters as git pre-commit hook and I have changed my python version as
brew list | grep python
python#3.7
python#3.9
brew unlink python#3.7
brew unlink python#3.9
brew link python#3.7
python -V
Python 3.7.9
and know seems something get broken and on git commit I get env: python3.9: No such file or directory, so what is env? and how I can edit it to use python#3.7?
In .git/hooks/pre-commit I have
#!/usr/bin/env python3.9
and running pre-commit install fixed it to #!/usr/bin/env python3.7
Even though #mrgloom answer pointed me in the right direction, it wasn't good enough to solve my situation.
This error just happened to me after I upgraded from Ubuntu 21.10 to 22.04. It clearly looks like the installed Python version isn't Python 3.9 anymore. So I quickly checked that right now I have 3.10.
Simply editing the .git/hooks/pre-commit with Python3.10 wasn't enough.
What worked for me was reinstall pre-commit: https://pre-commit.com/#install
So, you can either run pip install pre-commit or brew install pre-commit.
For me, I deleted the old virtual environment and re-Creating it and worked:
$ deactivate
$ python3 -m venv env
$ source env/bin/activate
Hope this would solve your problem, thank you!

Upgrading Python 3.7 to 3.9 on macOS Big Sur

I'm trying to upgrade Python 3.7 to 3.9 on macOS Big Sur. I'm also trying to avoid losing packages that were installed on Python 3.7 and reinstalling them again on Python 3.9
I tried using
brew install python3
brew update && brew upgrade python
which yielded
Already up-to-date.
Warning: python3 3.9.1_7 already installed
However when I run python3 --version it yields Python 3.7.0
Is this an issue with the alias? Is there a way to uninstall Python 3.7 and keep Python 3.9?
Running brew link python3 yields
Linking /usr/local/Cellar/python#3.9/3.9.1_7...
Error: Could not symlink bin/2to3
Target /usr/local/bin/2to3
already exists. You may want to remove it:
rm '/usr/local/bin/2to3'
To force the link and overwrite all conflicting files:
brew link --overwrite python#3.9
To list all files that would be deleted:
brew link --overwrite --dry-run python#3.9
I fixed this frustrating error by first removing the Python 3.7 manually, by deleting it from the Applications folder and then uninstalling Python 3.9 using brew uninstall python3
Next, I downloaded and installed the latest Python from here and it worked!
To save all the installed packages by generating a requirements file, Run
python3 -m pip freeze > requirements.txt
and to install them in another environment, Run
python3 -m pip install -r requirements.txt
I suggest use official binaries:
Download version you need from the python.org
Call the .pkg
Invoke Update Shell Profile.command script under /Applications/Python\ 3.XX/
After all reboot your terminal
Check your Python version. The old version and dependencies remain intact.

How to not update homebrew automatically when brew install some packages?

I installed python3 with homebrew on my Mac. However, the latest Python3.6 cannot work well with some packages, so I decide to roll back to the 3.5.2.
Since homebrew/versions has been deprecated,I checkout the commit in the directory: /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula
git log python3.rb
There
commit ec545d45d4512ace3570782283df4ecda6bb0044
Author: BrewTestBot <brew-test-bot#googlegroups.com>
Date: Tue Oct 11 06:42:06 2016 +0100
python3: update 3.5.2_3 bottle.
Then
git chekcout ec545
brew install python3
However, it seems that homebrew update itself automatically I run $brew install [Formula]
➜ Formula git:(ec545d4) brew install python3
Updating Homebrew...
How to not update homebrew automatically when brew install some packages?
Or how to install python 3.5.2 with homebrew?
Answer for the first question, How to not update homebrew automatically when brew install some packages?
hack code solution:
vim /usr/local/Homebrew/Library/Homebrew/brew.sh
Add return after line
update-preinstall() {
to:
update-preinstall() {
return
environment variable solution:
export HOMEBREW_NO_AUTO_UPDATE=1
or
export HOMEBREW_AUTO_UPDATING=0
or
export HOMEBREW_UPDATE_PREINSTALL=0
if U will always work, add it to .bash_profile
$ brew tap derekkwok/python or (zoidbergwill/python)
$ brew versions python
$ brew install python35
If you have already installed the older version of the formula you can simply switch the symlinks to reference it using a brew command.
brew switch python 3.5.2
If you want to keep a certain version and stop it from being updated ,you can pin a formula.
brew pin python
Also you can try this Python Version Management pyenv.
brew install pyenv
pyenv install 3.5.2
By the way,you can see homebrew-install-specific-version-of-formula to learn more.

How to pip install packages into different versions of Python

I have 2 Python versions
Python 3.4.3
Python 2.7.10
Env variable works with Python 3.4(in my system), so when I pip install*package_name it will only install the package into Python 3.4
I have a system variable for Python 2.7 -- %python27% -- also.
My question is; how can I pip install a package/module into Python 2.7 without changing the Env. Variable.
Note: %python27% pip install *package_name doesn't work.
Thank you.
You should have multiple executables of pip.
Use pip2 and pip3 interchangeably.
Anyway, you should consider using virtualenv package, initialize it like virtualenv -p /usr/bin/python2.7 env_name or virtualenv-3.4 -p /usr/bin/python3.4 env_name then each time you use your code, type source env_name/bin/activate and "python" should be aliased to virtualised version.
You can use pip for python2 and pip3 for python3.
Also you can try using virtualenv or pyenv
I had the same problem, but it was installing to Python 2.7 rather than Python 3.4. Using $ pip3 install *package_name solved the issue.

Installing pip with correct python version

I am on shared hosting and I need to install pip with the correct python version, 2.7. To install pip, I did:
$ easy_install pip
However, after it was installed I get the following:
[dave#web1 lib]$ pip --version
pip 1.0.2 from /home/premiere/dave/financials/lib/pip-1.0.2-py2.7.egg (python 2.4)
How would I re-install pip to work on the python2.7 version, which is also installed on the machine?
[premiered#web1 ~]$ python --version
Python 2.6.6
Which is strange, since it is installing to python2.4.
You may want to create a virtualenv using -p /path/to/python-2.7.binary param, and then activate it. Then all stuff you installed using pip would be correctly into your virtualenv.
If multiple versions of python are installed on the system, then you should invoke the version you want when installing. i.e.
$ python27 easy_install pip
This creates a pip file in your path that contains the specified version of python in the hashBang line.

Categories

Resources