I installed both python 2.7.13 and python 3.6.2 with homebrew and updated my ~/.bash_profile as such:
# Homebrew
export PATH=/usr/local/bin:$PATH
Python3 was linked fine. However, "which python" would still give me
/usr/bin/python
while "which python2" produces
/usr/local/bin/python2
It looks like homebrew installed python 2 as "python2" and never linked "python" to the new installation. This is causing me a lot of trouble down the road when installing virtualenvwrapper etc.
By the way, I also did
brew link python.
Anybody know why this is and how to fix it?
Much appreciated!!
They change that behavior here
Today I’d like to announce Homebrew 1.3.0. The most significant change
since 1.2.0 is that brew install python no longer installs a python
binary without manual PATH additions and instead installs a python2
binary. This avoids overriding the system python binary by default
when installing Python as a dependency. It also paves the way to
eventually have python be Python 3.x.
You will have to symlink python to the version of python installed by homebrew that you want.
You can do:
$ln -s /usr/local/bin/python2 /usr/local/bin/python
To symlink python to the homebrew Python2.x installation or do:
$ln -s /usr/local/bin/python3 /usr/local/bin/python
to link it to the Python 3.x hombrew installation.
gsi-frank's solution solves the problem quite well but after using his solution, you might encounter problems with your pip3. Your pip3 might be linked to the old instance and therefore unusable for your new python instance.
Type which pip3 to see where your pip3's path.
If your path is linked to your new instance, then ignore the rest of this answer.
Type echo $PATH to see your current path. If it contains your old python instance's path then you will need to remove it from your .bash_profile
To solve this: check your .bash_profile and see if there is a path to your old python instance.
If the old path is in there, you can delete it.
I had this problem and here's a link to the question if needed:
How to change pip3 path after installing python with homebrew?
To find your .bash_profile:
Go to your Finder
Right click and click Go to a folder... and type ~
Press CMD + shift + . to see hidden files
Right click your .bash_profile and open in a text editor
Related
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".
short Q,
In a mac OS sierra terminal,
If I do:
whereis python
/usr/bin/python
Then if I do:
/usr/bin/python it opens python 2.10
but if I execute
python it opens python 2.7.8.
and this one comes from
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
How do I change the default python to point to python 2.7.8? In the terminal and in intelliJ?
Check the PATH environment variable with
echo $PATH
The python version you get when typing bare 'python' will be the first one found in that list of directories.
It is possible to control which python version is launched by, for example, rearranging the entries in PATH or by adding a symbolic link to the desired version in a position before the current version.
However, a more popular way to manage multiple python versions on the same machine is to use virtualenv. This will give you much less headaches when using pip to install/uninstall packages for particular python versions.
As wim mentioned you will get the first python that is found in $PATH.
A nice way may be to ln -s /Library/Frameworks/Python.framework/Versions/2.7/bin/python /usr/local/bin/python. This will create a symbolic link in /usr/local/bin
That way you don't change the order in your $PATH variable. Also note that if you echo $PATH, /usr/local/bin should be before /usr/bin - in case you have other versions in /usr/bin (which you do given your example)
I would strongly recommend you do what wim mentioned and use virtualenv to manage you packages.
After installing Homebrew using the script on their homepage and checking if everything was alright with brew doctor, I issued brew install python3 in order to install Python 3 on my Mac.
Everything seemed fine until I tried running python3 --version; I ended up getting:
-bash: /Library/Frameworks/Python.framework/Versions/3.5/bin/python3: No such file or directory
I checked in the file directory to see what was going on and indeed, I didn't see any files pertaining to Python in my framework folder. It also looks like Python 2.7 isn't on my Mac either.
This is what I got after installing Python 3:
Summary
🍺 /usr/local/Cellar/python3/3.5.1: 3,438 files, 51.5M
edit_2: maybe this has something to do that there is no Python framework? I just read this off the Python website:
The Apple-provided build of Python is installed in /System/Library/Frameworks/Python.framework and /usr/bin/python, respectively. You should never modify or delete these, as they are Apple-controlled and are used by Apple- or third-party software. Remember that if you choose to install a newer Python version from python.org, you will have two different but functional Python installations on your computer, so it will be important that your paths and usages are consistent with what you want to do.
I think I detected what the problem is.
I guess that, at a certain moment, you had installed python from the official site instead of via Homebrew.
In my case, I installed it via the official website Python 3.6.4. A few months later, I wanted to upgrade it and noticed that it was very complex. So, I decided to move to Homebrew. Open a terminal window and let's try to fix this:
First, let's uninstall previous Python versions:
sudo rm -rf /Library/Frameworks/Python.framework
sudo rm -rf /usr/local/bin/python3
Then, remove the previous frameworks from the $PATHvariable:
nano ~/.bash_profile
You will see something like that:
# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH`
This is the problem: These paths don't exist. Comment the $PATH editions (or erase them):
# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
# PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
# export PATH
# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
# PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
# export PATH
Restart the computer and install via Homebrew Python 2 and 3:
brew update
brew install python
brew install python3
This worked for me. Now, if type python3 --version I get Python 3.7.0, and everything works fine :)
I had the same issue. I learned how to fix it for good:
Open "Applications" in Mac Finder and drag Python to the trash bin.
Empty the trash bin
If you have an error as above, then an official Python installation has been performed (as others have mentioned) via e.g. Python.org. This creates some kind of alias for the python or python3 commands outside a Bash alias. So while the command where python3 may point to /usr/local/bin/python3, python3 will still try to call /Library/Frameworks/Python.framework/Versions/3.5/bin/python3.
Note:
the MacOS system Python is /usr/bin/python
Homebrew Python(s) will be located in /usr/local/bin/
Pythons installed as an Apple application live in /Library/Frameworks/Python.framework/
Okay, this is what I gathered:
Don't delete the Python framework!
If it's deleted, then python3 --version won't work
Just install Python from the Python website
The framework will return and python3 --version will work
This error:
-bash: /Library/Frameworks/Python.framework/Versions/3.5/bin/python3: No such file or directory
suggests a remnant of some previous (attempt at an) installation of Python 3 using a different way (not Homebrew).
(I think this is actually where the Python installation from www.python.org goes. I wouldn't know though, as I've either never tried that package, but only installed the www.python.org version from source. This would suggest, though, that you already had an attempt at installing Python 3.5, something failed, and you're now trying Homebrew instead.)
I'd suggest moving (renaming) this out of the way, so your system doesn't pick it up. Something like
mv /Library/Frameworks/Python.framework/Versions/3.5 /Library/Frameworks/Python.framework/Versions/3.5-aside
(if there other versions of Python 3 in that directory, you may want to do the same for those.)
Also check that python3 isn't an alias. Commands such as
which python3
type python3
alias python3
will reveal that.
With the interfering Python 3 out of the way, try re-installing Python 3 through homebrew again. You may have to do an uninstall + reinstall.
Read carefully any homebrew messages once the installation is done, in particular if it mentions something about linking files: you may need to run something like brew link python3.
In attempting to get started learning and developing python, I've tried to follow the Python Guide to installing python on OS X, but haven't found it particularly "noob friendly." I have a new MacBook (Mtn. Lion - OS X 10.8.3) wich comes with Python 2.7.2 built in. But the guide advises installing a "framework-style build" via homebrew. So:
I installed homebrew via ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
Then it tells you to add homebrew to the top of your PATH by adding it to your ~/.bashrc file. ls -a showed that I did not have a ~/.bashrc file in my home directory. After searching Stack Overflow on how to do that, I ran nano ~/.bashrc, and inserted the line export PATH=/usr/local/bin:$PATH to the file and saved the file.
I then ran brew install python --framework and the install completed.
Then, the guide says to "add the new Python scripts directory to your PATH" so, I'm assuming that means I need to add the line it provides to my ~/.bashrc file also. So, I added export PATH=/usr/local/share/python:$PATH to my ~/.bashrc file above my previous entry.
Finally, this is where I run into trouble, it says to easy_install pip. However, when I do that I get an error 13.
So, here are the things I need some help with.
Was I correct in my assumptions about how to add homebrew and python scripts to my PATH?
Did I do something wrong or do I just need to use sudo to install pip? (I'm really sorry if the answer is already on this page but even those answers don't make total sense to me and I want to be careful and not screw something up)
After installing the framework-style build of python (which I believe was the current 2.7.3), how come running python in my terminal still shows v2.7.2?
Thanks! I appreciate any help.
I've tried to follow the Python Guide to installing python on OS X,
but haven't found it particularly "noob friendly.
Yes, I think it is misleading/outdated.
Then it tells you to add homebrew to the top of your PATH by adding it
to your ~/.bashrc file. ls -a showed that I did not have a ~/.bashrc
file in my home directory. After searching Stack Overflow on how to do
that, I ran nano ~/.bashrc, and inserted the line export
PATH=/usr/local/bin:$PATH to the file and saved the file.
On the Mac, just use ~/.profile
I then ran brew install python --framework and the install completed.
I think you don't need the --framework option unless you want to replace your Mac OS default installation and need an Mac OS Framework-style directory layout. There is no need to replace it though, the homebrew installation will take precedence anyway.
Then, the guide says to "add the new Python scripts directory to your
PATH" so, I'm assuming that means I need to add the line it provides
to my ~/.bashrc file also. So, I added export
PATH=/usr/local/share/python:$PATH to my ~/.bashrc file above my
previous entry.
Again, do it in ~/.profile. And don't forget to do a
source ~/.profile
otherwise the changes will only become active in any new terminal window, not the one you are currently using.
Finally, this is where I run into trouble, it says to easy_install
pip. However, when I do that I get an error 13.
The error shows that you try to install it your Mac OS system's default Python library (rather than in /usr/local, homebrew style), which would require root privileges. Just don't.
Also, with homebrew python, pip is already installed.
Check your path:
$ which pip
/usr/local/bin/pip
$ ls -l /usr/local/bin/pip
[..] /usr/local/bin/pip -> ../Cellar/python/2.7.3/bin/pip
Added bonus: Then do
pip install virtualenv
and use that.
And to your questions:
Was I correct in my assumptions about how to add homebrew and python
scripts to my PATH?
Yes, but use .profile and do a source .profile afterwards.
Did I do something wrong or do I just need to use sudo to install pip?
(I'm really sorry if the answer is already on this page but even those
answers don't make total sense to me and I want to be careful and not
screw something up)
You don't need sudo with homebrew, and pip is installed automatically with homebrew python.
After installing the framework-style build of python (which I believe
was the current 2.7.3), how come running python in my terminal still
shows v2.7.2?
Probably PATH not correct, do echo $PATH and check that it is correct. That is unrelated to being "framework-style" or not, though.
If you installed python with homebrew, you should already have pip installed. Try running
pip --version
to see whether and where pip is installed. Hopefully it's in a /usr/local/... path where your other homebrew things live.
Also before you install too much more with homebrew be sure to run these commands:
brew update
brew doctor
Good luck!
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.