change python version in terminal and intelliJ - 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.

Related

Editors not honoring Python shell aliases

I run macOS Catalina with zshell.
Out of the box the os has one python2 and one python3 version in /usr/bin/python and /usr/bin/python3. I have installed a newer python3 via Homebrew. That version is in /usr/local/opt/python#3.8/bin/python3.
I have added aliases to my ~/.zshrc-file so that both python and python3 will launch into the 3.8 Homebrew version.
When using editors (e.g. Atom) that run python scripts by calling python3 this aliasing does not seem to work. I guess this is because it is specific to the terminal shell.
What is a better way of getting my homebrew python3.8 to become the default python on my system?
Don't uses aliases for selecting alternate programs. Use your PATH variable to manage your preferences.
Start by creating a local bin directory if you don't already have one.
mkdir -p ~/bin
Assuming your PATH is already set up to prefer Homebrew versions over system-installed versions, add ~/bin to the front of the path.
# In .bash_profile
PATH=~/bin:$PATH
Now, create a symbolic link ~/bin/python to the desired Python 3 interpreter.
ln -s /usr/local/opt/python#3.8/bin/python3 ~/bin/python
Now when you run python, you'll get your Homebrew python3.8 interpreter. You can still access the system Python 2 with /usr/bin/python when needed. Your editors should also inherit and respect your PATH variable, unless it is configured to use a specific hard-coded path.
Note that Homebrew still(?) links /usr/local/bin/python to its own Python 2 interpreter; I don't recommend changing that to python3, lest other Homebrew-managed programs get Python 3 when they require Python 2, hence the use of ~/bin. (There's still a chance that programs using python via path lookup will assume it is Python 2, but this should minimize the problems.)

Make python3 as my default python on Mac

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".

Homebrew not linking python correctly?

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

Always run python 2.6 instead of 3.4 in centos

I have installed python 3.4 in centos.
So I opened terminal and typed python or idle then always ran python 2.6 not python 3.4.
How can i run python 3.4 instead of 2.6?
thanks you.
Do not replace the default Python! CentOS's system tools such as yum, system-config-* tools and several other things rely on the default Python 2.6 installation. Set up a virtual environment instead, where you can define which is the default version.
virtualenv --python=/usr/bin/python3.4 myenviron
source myenviron/bin/activate
First, look for where your Python 3.4 is located:
$ which python3.4
/usr/bin/python3.4
See if ~/bin (e.g., /home/username/bin) directory is in the PATH environment variable:
$ echo $PATH
/home/username/bin:/usr/local/bin:/usr/bin:/bin
If not, add the bin directory to PATH, ideally within your ~/.bashrc file. Then create a symlink pointing to the Python interpreter under the bin directory:
$ ln -s /usr/bin/python3.4 /home/username/bin/python
This way, when you type python on the command line, the interpreter specified will be launched.
I am on Debian/Wheezy, so the detail might be slightly different, but a similar approach should work.

How to make python_select work for '$>python' command?

I installed a couple of pythons in different versions with macports, and the apple python 2.6 is also working. Now I need to run a program which requires MySQLdb package support in python, and this package was installed to the python I installed by macports. The program tells me that there is no MySQLdb installed, so I guess it is the apple python working for that program.
I searched for some help and found python_select for switching between pythons. However after the command
$>sudo python_select python25
told me that it selected the version "python25" for python, when I type
$>python
it is still apple python 2.6 that launches.
The question is that how can I make python25(the one with MySQLdb) work for the program rather than apple python?
Another important thing, the program is NOT a .py file and needs to be compiled before running. So do I need to re-install this program? My Mac OS version is Snow Leopard 10.6.
Any answer is appreciated.
By default, MacPorts installs user programs (or links to them) in /opt/local/bin. The MacPorts select_python command selects which python instance is linked to /opt/local/bin/python. It has no effect (nor should it) on what Apple installs in /usr/bin, which is where the Apple-supplied python and python2.x commands are.
To invoke the MacPorts python2.5, you either need to ensure that /opt/local/bin precedes /usr/bin on your shell $PATH (you can do this by modifying your .bash_profile or other shell initialization script) or you can simply invoke the desired python with an absolute path reference:
$ /usr/bin/python your-program.py
to use the Apple-supplied default python;
$ /opt/local/bin/python your-program.py
to use the version selected with python_select, or:
$ /opt/local/bin/python2.5 your-program.py
to explicitly select the MacPorts 2.5 one.
EDIT:
To modify your search PATH to use MacPorts, add this line to .bash_profile:
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
First, I am not sure with Mac, coz I never use it before.
but in Linux, when I do whereis python
It will show like /usr/bin/python /usr/local/bin/python ....etc
in my .bashrc file, I just export PATH=/usr/local/bin:/usr/bin:$PATH when I want /usr/local/bin more priority
or you still can run like
/usr/bin/python yourpython.py
or
/usr/local/bin/python yourpython.py
depends on your python install locations
just my 2 cents. sorry if my answer dont make you any helps.
'python' in the Mac is just a link. Do a 'which python', 'cd' to the directory in which 'python' resides, and then do an 'ls -a py*'. You shall see where python is pointing too. If you want that python to point to your different version of python, just make it link to the right version.

Categories

Resources