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".
Related
I recently installed python3 on my vps, I want to enable it as default, so that when I type
python I get python 3. I think the problem is its installed in /usr/local/bin instead of /usr/bin/ typing python on the terminal access python2 typing python3 returns bash: python3: command not found
Most answers I have seen is a bit confusing as I am not a centos expert.
for a simple fix, you can use alias
add the alias to your .bashrc file
sudo vi ~/.bashrc
then add your alias at the bottom
alias python="python3.9"
So that when you type python you'll get python 3
There're several ways.
First, check if /usr/local/bin isn't in your $PATH variable:
echo $PATH
If it is indeed not there, you may want to add this line to your .bashrc file (assuming you're using bash):
export PATH="/usr/local/bin:$PATH"
This will add /usr/local/bin to your $PATH variable and will make python3 accessible after you relaunch a terminal session.
Now, changing the default python globally might not be a good a idea as there could be system software depending on python command pointing to python2.
What you could do is use PyEnv which will allow you using different python versions on your computer: https://github.com/pyenv/pyenv
I am installing Python 3.7.2 for the first time, and I'm using the VS Code python extension.
When I run python -V I get Python 2.7.10 which is not correct!
When I select the usr/local/bin/python3 interpreter in VS Code I get this error when running a script:
bash: /Users/erik/Work/Python/usr/local/bin/python3: No such file or directory
But when I look in usr/local/bin I can see that Python3 is there. I'm not sure why VS Code pastes the work directory in front of usr/local/bin ?
My first thought was that Python3 should be in the PATH variable so I ran the included Update Shell Profile command, which gives this feedback:
This script will update your shell profile when
the 'bin' directory of python is not early enough
of the PATH of your shell.
All right, you're a python lover already
Now, after rebooting VS Code I get a new option for selecting an interpreter:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
Is that different from the Python in usr/local/bin ? When I select it, I get this error:
The script isort is installed in '/Users/erik/Library/Python/3.7/bin' which is not on PATH.
I also get this sideways related error:
You are using pip version 18.1. You should consider upgrading via the 'pip install --upgrade pip' command.
But, when following these instructions I get yet another error:
bash: pip: command not found
All in all, this process and the official documentation seem less than user-friendly? Apparently I'm required to dig deep through my mac's system files in the terminal before even writing one line of code.
Am I missing an essential step here?
I suggest that you use virtual environment for your project
first
pip install virtualenv
open cmd in your project directory that you open in VS-Code (it's important that vs-code sees this virtualenv folder that we will create)
mkvirtualenv my_env
and it will activate it automatically. if not run
my_env/bin/Scripts/activate or my_env/Scripts/activate
Then go open vs-code then select my_env for python interpreter
Well, if you want to change your default Python version for the whole system, it might break some applications that are depending on Python 2.
You can alias the commands by adding this line to your ~/.bash_profile:
$ alias python='python3'
The python command will now refer to python3.
If you want to execute the original Python (which refers to python2), you can escape the alias (so \python will launch python2 without touching the alias).
Btw.
$ unlink /usr/local/bin/python
$ ln -s /usr/local/bin/python3.7 /usr/local/bin/python
could also be a workaround for you.
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
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