I try to switch to Homebrew (after using fink and macport) on Mac OS X 10.6.2. I have installed python 2.7 with
brew install python
The problem is that, contrary to Macport, it seems that there is no python_select utility, and my default mac python is always default
which python
give me
/usr/bin/python
and /usr/bin/python is not a symlink
How can I do to make python brew flavour to be my default python ?
As suggested by the homebrew installer itself, be sure to add this to your .bashrc or .zshrc:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
As you are using Homebrew the following command gives a better picture:
brew doctor
Output:
==> /usr/bin occurs before /usr/local/bin This means that system-provided programs will be used instead of those provided by
Homebrew. This is an issue if you eg. brew installed Python.
Consider editing your .bash_profile to put: /usr/local/bin ahead of
/usr/bin in your $PATH.
See: How to symlink python in Homebrew?
$ brew link --overwrite python
Linking /usr/local/Cellar/python/2.7.3... 28 symlinks created
$ which python
/usr/local/bin/python
Quick fix:
Open /etc/paths
Change the order of the lines (highest priority on top)
In my case /etc/paths looks like:
/usr/local/bin
/usr/local/sbin
/usr/bin
/bin
/usr/sbin
/sbin
If you want to know more about paths in OSX I found this article quite useful:
http://muttsnutts.github.com/blog/2011/09/12/manage-path-on-mac-os-x-lion/
I did "brew install python" for OSX High Sierra. The $PATH had /usr/local/bin before any other path but still which python was pointing to the system's python.
When I looked deeper I found that there is no python executable at /usr/local/bin. The executable is named python2. To fix this problem create a symbolic link python pointing to python2:
/usr/local/bin $: ln -s python2 python
For Apple Silicon machines, the path are slightly different. After running brew install python, you must ensure your ~/.zshrc uses the correct Homebrew paths:
# Homebrew
eval "$(/opt/homebrew/bin/brew shellenv)"
# Homebrew: Python
export PATH="/opt/homebrew/opt/python/libexec/bin:$PATH"
Results:
% which python
/opt/homebrew/opt/python/libexec/bin/python
% python --version
Python 3.9.9
% which pip
/opt/homebrew/opt/python/libexec/bin/pip
% pip -V
pip 21.3.1 from /opt/homebrew/lib/python3.9/site-packages/pip (python 3.9)
python formula now uses python3(v3.6.5 for now), brew will link the directory:
/usr/local/opt/python -> ../Cellar/python/3.6.5
it will also link the binary:
/usr/local/bin/python3 -> ../Cellar/python/3.6.5/bin/python3
If you still need to use python2.x, use:
brew install python#2
To use homebrew's python, just put its directory in PATH, for bash:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
for fish:
set -x PATH /usr/local/opt/python/libexec/bin $PATH
Note:
doing this will shadow the system default version of python
homebrew used to link python to /usr/local/share/python in older versions.
Homebrew does NOT replace stuff in "/usr/bin". You'll just want to put "/usr/local/bin" ahead of "/usr/bin" in your path, then "which python" will give you "/usr/local/bin/python".
Replacing /usr/bin/python (or /usr/bin/ruby) is highly unrecommended.
Modify your $PATH, Add this in your bashrc or bash_profile:
export PATH=/usr/local/bin:/usr/local/sbin:~/bin:$PATH
more click here:
Issue #89791
I did brew install python, my $PATH was good, but still, which python gave me the system installed one. Restarting the terminal fixed it.
You need to edit your PATH environmental variable to make sure wherever the homebrew python is located is searched before /usr/bin. You could also set things up in your shell config to have a variable like PYTHON be set to your desired version of python and call $PYTHON rather than python from the command line.
Also, as another poster stated (and especially on mac) DO NOT mess with the python in /usr/bin to point it to another python install. You're just asking for trouble if you do.
python now points to python3, if you need python 2 then do:
brew install python#2 and then in your .zshrc or .bashrc file
export PATH="/usr/local/opt/python#2/libexec/bin:$PATH"
Now, pyhon --version = Python 2.7.14 and python3 --version = Python 3.6.4.
That's the behavior I'm used to seeing in my terminal.
I believe there are means to make homebrew python default, but in my opinion the proper way to solve a problem is not to mess with system python paths: it is better to create a virtualenv in which homebrew python would be default (by using virtualenv --python option). Using tools like python_select is almost always a bad idea.
Use pyenv instead to install and switch between versions of Python. I've been using rbenv for years which does the same thing, but for Ruby. Before that it was hell managing versions.
Consult pyenv's github page for installation instructions. Basically it goes like this:
- Install pyenv using homebrew. brew install pyenv
- Add a function to the end of your shell startup script so pyenv can do it's magic. echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
Use pyenv to install however many different versions of Python you need. pyenv install 3.7.7.
Set the default (global) version to a modern version you just installed. pyenv global 3.7.7.
If you work on a project that needs to use a different version of python, look into pyevn local. This creates a file in your project's folder that specifies the python version. Pyenv will look override the global python version with the version in that file.
Add the /usr/local/opt/python/libexec/bin explicitly to your .bash_profile:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
After that, it should work correctly.
Just do:
brew install python
brew link python
After doing that, add this to your bashrc or bash_profile:
alias python='/usr/local/bin/python2'
Enjoy!
You can edit /etc/paths. Here is mine:
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
Then add a symlink for the python version. In my case
$ cd /usr/local/bin
$ ln -s python3 python
Voila!
If you are fish shell
echo 'set -g fish_user_paths "/usr/local/opt/python/libexec/bin" $fish_user_paths' >> ~/.config/fish/config.fish
try this
which python3
Try typing python3 instead of just python
Greeting folks! I have the need to use python 3.10 version to harness its new features. My solved solution at 2022-07-03 is as follows. Have fun coding python!
» rm '/usr/local/bin/pip3.10'
» brew link python#3.10
» echo 'export PATH="/usr/local/opt/python#3.10/bin:$PATH"' >> ~/.zshrc
» python3
Python 3.10.5 (main, Jun 23 2022, 17:15:25) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Since High Sierra, you need to use:
sudo chown -R $(whoami) $(brew --prefix)/*
This is because /usr/local can no longer be chowned
brew link python
And you must create/add an alias for python and put it in your .zprofile (Located in Users/username folder, if you press Shift+command+.
This must point to your homebrew python installation location.
alias python ='opt/homebrew/bin/python3'
No idea what you mean with default Python. I consider it bad practice to replace the system Python interpreter with a different version. System functionality may depend in some way on the system Python and specific modules or a specific Python version. Instead install your custom Python installations in a safe different place and adjust your $PATH as needed in order to call you Python through a path lookup instead of looking for the default Python.
Related
Since I got the macOS v12.3 (Monterey) update (not sure it's related though), I have been getting this error when I try to run my Python code in the terminal:
I am using Python 3.10.3, Atom IDE, and run the code in the terminal via atom-python-run package (which used to work perfectly fine). The settings for the package go like this:
The which command in the terminal returns the following (which is odd, because earlier it would return something to just which python):
I gather the error occurs because the terminal calls for python instead of python3, but I am super new to any coding and have no idea why it started now and how to fix it. Nothing of these has worked for me:
I deleted and then reinstalled the Python interpreter from python.org.
I tried alias python='python3' (which I saw in one of the threads here).
I tried export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" (which I found here).
To reset zsh and paths, I deleted all associated hidden files in /local/users/ and ran the terminal once again.
I deleted everything and reinstalled Mac OS X and the Python interpreter only to get the same error.
Anyone updating their macOS to Monterey 12.3 will find that they suddenly no longer have the system-provided Python 2.
The reason for this is that Apple removed the system-provided Python 2 installation (details).
So a workaround/solution for this is to use pyenv to install Python 2.7 (or any other specific version you need).
Install pyenv with brew to manage different Python versions: brew install pyenv
List all installable versions with pyenv install --list
Install Python 2.7.18 with pyenv install 2.7.18
List installed versions with pyenv versions
Set global python version with pyenv global 2.7.18
Add eval "$(pyenv init --path)" to ~/.zprofile (or ~/.bash_profile or ~/.zshrc, whichever you need)
Relaunch the shell and check that Python works, or run $ source ~/.zprofile (Thanks masoud soroush!)
OK, after a couple of days trying, this is what has worked for me:
I reinstalled Monterey (not sure it was essential, but I just figured I had messed with terminal and $PATH too much).
I installed python via brew rather than from the official website.
It would still return command not found error.
I ran echo "alias python=/usr/bin/python3" >> ~/.zshrc in terminal to alias python with python3.
Problem solved.
As far as I get it, there is no more pre-installed python 2.x in macOS as of 12.3 hence the error. I still find it odd though that atom-python-run would call for python instead of python3 despite the settings.
If you simply installed Python 3, just use python3 as the command instead of just python. In my case, I had to install pynev first via Homebrew (executable brew) using brew install pyenv.
But still after using pynev to install Python 2.7.18 and setting it as a global version using pyenv global 2.7.18, I still ran into an error while trying to run python.
What worked for me (since I already had Python 3 installed) was by changing my command to use python3 instead of just python. Of course, this won't be a solution to everyone who may want to use Python 2.
you should try
sudo ln -s /usr/bin/python3 /usr/local/bin/python
that solve my problem:)
I just had this problem on a new Macbook Pro with macOS Monterey, and the below worked for me using Homebrew.
Using alias is not necessary when using Pyenv. Tested with Atom 1.60.0 and atom-python-run 0.9.7.
Install pyenv (https://github.com/pyenv/pyenv#installation) and its dependencies (https://github.com/pyenv/pyenv/wiki#suggested-build-environment):
brew install pyenv
brew install openssl readline sqlite3 xz zlib tcl-tk
Install Python 3.10.6 but I assume other 3.x versions should work as well:
pyenv install 3.10.6
Add Pyenv to your shell according to the instructions in https://github.com/pyenv/pyenv#set-up-your-shell-environment-for-pyenv. In your home directory:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
Make the installed Python available everywhere (this can be overridden per project or folder, if necessary):
pyenv global 3.10.6
Now, the output looks like this:
% which python
/Users/jl/.pyenv/shims/python
% python --version
Python 3.10.6
If some scripts still fail, check that you have added Pyenv to the necessary shell startup file(s) as mentioned in step 3 above.
Since you installed python3.9> (https://python.org/downloads/) , you just need to run everything with python3 instead of python. You don't have to downgrade your python to use it... It works fine with suffixing python with 3, i.e. python3 eg.
$ which python3, python3 --version, $ python3 script.py etc
I installed Python then this error occurred and I just run
echo "alias python=/usr/bin/python3" >> ~/.zshrc
and I t worked.on macOS Monterey 12.4, Python 3.8.9
Try the command python3 --version if you have installed Python 3.
I had the same problem with fresh MacOS Ventura with Apple Silicon M1 Pro chip. Seems like brew installs to a different location than before on Apple Silicon?
/usr/bin/ folder had a Python executable and the version for it was 3.9.6.
So I installed Python using brew but then I had to execute it via python3 command so I ran echo "alias python=/opt/homebrew/bin/python3" >> ~/.zshrc and restarted my terminal. I was prompted with the desired version of Python when executing python command.
alias python=/usr/bin/python3
This command works but need to re-enter whenever we restart the termian
If you are using pyenv and on Mac M1, then try switching the python from system version to a version that is installed already (ex: pyenv global 3.8.13, considering 3.8.13 is another version). In order to check what versions installed use pyenv versions.
If you don't have another version, try installing a new one and switch to that new environment (ex: pyenv install 3.8.13). Then install using pip. Everything should work fine. thats what worked for me.
and make sure your pip and python are aliased to pip3 and python3 respective in the respective terminal configuration file.
I First installed python with brew
brew install python
Then I tried linking the python with symlinks using:
brew link python3
And it worked for me.
PS: I was trying to run a shell script and alias approach didn't work.
I got zsh: Command not found python after installing using https://www.python.org/ftp/python/3.10.4/python-3.10.4-macos11.pkg
I solved it by configuring the ENVIRONMENT. I added a line, alias python=/usr/local/bin/python3.10, into the .zshrc file (you must be sure it is the true path).
My macOS (Apple silicon):
Darwin Johns-MacBook-Pro.local 21.4.0 Darwin Kernel Version 21.4.0: Mon Feb 21 20:35:58 PST 2022; root:xnu-8020.101.4~2/RELEASE_ARM64_T6000 arm64
for 3.10.8
install with python with brew command
brew install python#3.10
if you have .zshrc file just edit like this
alias python='python3'
alias pip ='pip'
export PATH="/opt/homebrew/opt/python#3.10/libexec/bin:$PATH"
if you dont have .zshrc file:
cd
vi .zshrc
and copy/paste the line below
alias python='python3'
alias pip ='pip'
export PATH="/opt/homebrew/opt/python#3.10/libexec/bin:$PATH"
I have a MacBook Pro with an Apple M1 chip and macOS v12.4 (Monterey).
I deleted all Python versions and cleared the trash
Opened Terminal and typed "python"
This prompted an xcode.app update
Then I downloaded the latest version of Python from python.org (version 2.10.5)
Installed that version
Quit Terminal
Opened Terminal
Typed "python3"
It seems to now be there OK.
I use brew install brew install python#3.9, after installation success I want to know the version of python, I run command python --version, and I got the problem zsh: command not found: python.
If you install it successfully, please check in the terminal. It will show you if you got problem in install if not problem you just change command from python --version to python3 --version.
Just run the below command in your terminal:
echo "alias python=/usr/bin/python3" >> ~/.zshrc
Basically, here we are saying the terminal to treat python as python3.
Works like magic!
eval "$(pyenv init --path)"
bash starting will be more slow, just suggest
export PATH=$PATH:$HOME/.pyenv/shims
What I'm trying to do here is to make python3 as my default python. Except the python 2.7 which automatically installed on mac, I installed python3 with homebrew. This is the website that I'm following. http://docs.python-guide.org/en/latest/starting/install3/osx/#install3-osx
I guess I followed every instruction well, got xcode freshly installed, Command line tools, and homebrew. But here's my little confusion occurs.
The script will explain what changes it will make and prompt you before the installation begins. Once you’ve installed Homebrew, insert the Homebrew directory at the top of your PATH environment variable. You can do this by adding the following line at the bottom of your ~/.profile file
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
I was really confused what this was, but I concluded that I should just add this following line at the bottom of ~/.profile file. So I opened the ~/.profile file by open .profile in the terminal, and added following line at the bottom. And now it looks like this.
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
# Setting PATH for Python 3.6
# The original version is saved in .profile.pysave
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
And then I did brew install python, and was hoping to see python3 when I do python --version.
But it just shows me python 2.7.10. I want my default python to be python3 not 2.7
And I found a little clue from the website.
Do I have a Python 3 installed?
$ python --version
Python 3.6.4
If you still see 2.7 ensure in PATH /usr/local/bin/ takes pecedence over /usr/bin/
Maybe it has to do something with PATH? Could someone explain in simple English what PATH exactly is and how I could make my default python to be python3 when I run python --version in the terminal?
Probably the safest and easy way is to use brew and then just modify your PATH:
First update brew:
brew update
Next install python:
brew install python
That will install and symlink python3 to python, for more details do:
brew info python
Look for the Caveats:
==> 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
Then add to your path /usr/local/opt/python/libexec/bin:
export PATH=/usr/local/opt/python/libexec/bin:$PATH
The order of the PATH is important, by putting first the /usr/local/opt/python/libexec/bin will help to give preference to the brew install (python3) than the one is in your system located in /usr/bin/python
Before we make the changes, the default version of python in my system was python 2.7.17.
python --version
Python 2.7.17
To make python3 as default python by replacing python2 in Ubuntu.
Open Terminal
cd
nano ~/.bashrc
alias python=python3 (Add this line on top of .bashrc file)
Press ctr+o (To save the file)
Press Enter
Press ctr+x (To exit the file)
source ~/.bashrc OR . ~/.bashrc (To refresh the bashrc file)
python --version
Python 3.7.5
Changing the default python version system wide can break some applications that depend on python2. The alternative solution would be to create an alias.
If you are using zsh (the default on Mac OS) run the following from terminal:
echo 'alias python="python3"' >> ~/.zshrc
echo 'alias pip="pip3"' >> ~/.zshrc
According to this S.O. post, changing the default Python interpreter could possibly break some applications that depend on Python 2.
The post also refers to using aliasing as a solution, and this link might also be a good reference on how to do that.
Personally, I just type "Python3" before I run scripts or go into a shell environment instead of "python".
I just used brew to install Python 3 on OS X. The python3 command now starts the interpreter using brew Python 3.6, but python still opens the interpreter with the default system Python 2.7.
My understanding was that, by default, brew Python should now override system Python. (I.e., see Order of /usr/bin and /usr/local/bin and more in $PATH). In my PATH, /usr/local/bin comes before /usr/bin, so it shouldn't be a PATH issue. I have tried restarting Terminal, with no effect.
Here is my full PATH in case that is relevant.
/Users/**/.rvm/gems/ruby-1.9.3-p362/bin:/Users/**/.rvm/gems/ruby-1.9.3-p362#global/bin:/Users/**/.rvm/rubies/ruby-1.9.3-p362/bin:/Users/**/.rvm/bin:/Users/**/.rvm/bin:/Users/**/Python/PmagPy/programs/conversion_scripts2/:/Users/**/Python/PmagPy/programs/conversion_scripts/:/Users/**/Python/PmagPy/programs:/usr/local/heroku/bin:./bin:/usr/local/sbin:/usr/local/bin:/usr/local/share/npm/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin
Why isn't brew Python taking precedence? And how can I fix (or troubleshoot) this? If I can't find another option, I can create an alias, but I prefer to understand what's happening and get to the root of the problem.
Update:
I checked out the "possible duplicate" question, but my issue doesn't appear to be a linking problem:
~ brew link --overwrite --dry-run python
Warning: Already linked: /usr/local/Cellar/python/3.6.4_4
To relink: brew unlink python && brew link python
~
TL;DR Add the following to your .bash_profile (or equivalent):
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
Explanation
It seems python via homebrew is now handled differently (see https://docs.brew.sh/Homebrew-and-Python).
python3 points to Homebrew’s Python 3.x (if installed)
python2 points to Homebrew’s Python 2.7.x (if installed)
python points to Homebrew’s Python 2.7.x (if installed) otherwise the macOS system Python. Check out brew info python if you wish to add
Homebrew’s 3.x python to your PATH.
Checking out brew info python hints at what you need to do:
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
The hint being that you therefore have to add /usr/local/opt/python/libexec/bin before /usr/bin in your path (not /usr/local/bin as stated in some sources e.g. https://docs.python-guide.org/starting/install3/osx/)
See also https://github.com/Homebrew/homebrew-core/issues/15746
One-liner to get homebrew python working:
zsh
echo -n 'export PATH="/usr/local/opt/python/libexec/bin:$PATH"' >> ~/.zshrc && source ~/.zshrc
bash
echo -n 'export PATH="/usr/local/opt/python/libexec/bin:$PATH"' >> ~/.bashrc && source ~/.bashrc
Explanation:
>> filename appends at the end of the file
source filename reloads the file
I tried a few of the proposed solutions in How to link home brew python version and set it as default, but none of them worked. Ultimately I solved this by symlinking python3 --> python:
ln -s /usr/local/bin/python3 /usr/local/bin/python
I have 3 versions of python installed on my Mac. 2 of them are through brew i.e. python2 and python3 while the native version is python. the problem is when I put brew install python it installs python2 and not python. (By saying python means the version which runs on putting that command in terminal). What should I do so that if I type python my brew installed python launches.I have my path variables set correctly and the brew installation path is ahead than that of the usr/bin The problem I am encountering is that I have nltk installed through pip, pip2 and pip3 and when I import nltk in python2 and python3 there is no problem but when I do that in python it show no module found.
Try which python in a terminal to see which python will run. Then you know and can act accordingly to fix it.
ls -lsa $(which python) will let you see if it is a symlink to another location or a real executable. if a Symlink you can see where it points to and so you can follow the breadcrumbs to the final binary used.
if you run python from a terminal and in the python REPL do the following
import sys
print sys.path
you can see to which site-packages locations are pointed.
Other than that you of course have the option to use virtual environments to set up your version of python
if you really want python2 to be the default python command you can add a symlink to your ~/bin folder (create it if it does not exist)
mkdir ~/bin
cd ~/bin
ln -s $(which python2) python
chmod +x python
and make sure that export PATH=~/bin:$PATH is added at the back of your .bashrc or .profile or .zshrc file
Now start a new terminal session and try out python again it should point to brews version
Hope that helps
On my Fedora11 machine which has python2.6 pre-installed on it, I was able to successfully install python 2.7 using the following steps:
wget http://www.python.org/ftp/python/2.7/Python-2.7.tar.bz2
tar -xvjf Python-2.7.tar.bz2
cd Python*
./configure --prefix=/opt/python27
make
make install
vi ~/.bash_profile
## replaced PATH=$PATH:$HOME/bin
## with PATH=$PATH:$HOME/bin:/opt/python27/bin
## reload .bash_profile
source ~/.bash_profile
echo "/opt/python27/lib" > /etc/ld.so.conf.d/python27.conf
ldconfig
However, when I checked the python version the system uses via terminal (python -V), it still shows python 2.6.
How will I make the system use python2.7 as its default python?
Or if possible, how will I uninstall python2.6?
Thanks in advance!
Uninstalling the system Python is a bad idea. There are many other packages and softwares that depend on it. It'll be better that you use python2.7 by either modifying the $PATH or creating an alias e.g. python2.7 that points to the python that you installed in /opt dir.
Uninstalling fedora-provided python 2.6 might break many packages that depend on it. I advise you against doing it.
Now, your problem is simply that $PATH and similar variables ($MAN_PATH etc.) are searched from left to right. You appended your new /opt/python27/bin after standard locations like /usr/bin. Reverse the order, and you will get /opt/python27/bin/python as a default python binary.
First of all - never ever try to uninstall Python on RHEL/CentOS/Fedora. yum is written in Python and there will be many problems with repairing the system.
If you want the system to use Python2.7 by default, find where the Python2.6 (use whereis python or which python commands) binary is located, backup it and replace with the binary of Python2.7
Instead of uninstall older version, use specific version of python while using it
I changed symbolic link
ln -s /usr/local/bin/python2.7 /usr/local/bin/python
And used
python -m pip install pip --upgrade
Or you can simply use Yum feature of linux & run command yum remove python it will delete python & related dependencies from the system