Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

python vs. python3 command ...

Hi folks,

if I type python into my Mac terminal I get this:

Python 2.7.10 (default, Oct 23 2015, 18:05:06) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

but if I type python3 I get this:

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

I am really lazy and I just wonna type "python" and work with the version 3 of python. Can I manage it somehow?

Best wishes and thanks in advance

Grigorij

Sadly Python 2 is used for more stuff, so it is the default version. I am also always thinking of the same thing.

Hello everyone,

thanks to all for the great help.

So my python is inside /usr/bin/python and inside the binary folder there are this python directories:

  • python
  • python-config
  • python2.6
  • python2.6-config
  • python2.7
  • python2.7-config
  • pythonw
  • pythonw2.6
  • pythonw2.7

For python3 I get this location:

/Library/Frameworks/Python.framework/Versions/3.5/bin/python3

with this directories:

  • python3.5-config
  • python3
  • python3.5m-config
  • python3-32
  • python3-config
  • python3.5
  • python3.5-32

I have tried to remove the python symlink and have done this steps:

bash-3.2# pwd
/usr/bin
bash-3.2# rm python
override rwxr-xr-x  root/wheel restricted,compressed for python? 
bash-3.2# ln -s python3 python
ln: python: Operation not permitted
bash-3.2# 

Am I in the wrong directory?

And one another question:

is a symlink a text file that describes the location where a program is located? Something like typing REPL into a workspace in the Java course?

Thanks in advance

Grigorij

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Grigorij, can you post the results from:

ls -ld /usr/bin /usr/bin/python*

Hello Chris,

the output is :

drwxr-xr-x  1055 root  wheel  35870 Jan  4 16:16 /usr/bin
-rwxr-xr-x     1 root  wheel  66480 Dec  3 07:36 /usr/bin/python
-rwxr-xr-x     5 root  wheel    925 Aug 23  2015 /usr/bin/python-config
lrwxr-xr-x     1 root  wheel     75 Oct  4 19:21 /usr/bin/python2.6 -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6
lrwxr-xr-x     1 root  wheel     82 Oct  4 19:21 /usr/bin/python2.6-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6-config
lrwxr-xr-x     1 root  wheel     75 Oct  4 19:21 /usr/bin/python2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x     1 root  wheel     82 Oct  4 19:21 /usr/bin/python2.7-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
-rwxr-xr-x     1 root  wheel  66480 Dec  3 07:35 /usr/bin/pythonw
lrwxr-xr-x     1 root  wheel     76 Oct  4 19:21 /usr/bin/pythonw2.6 -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/pythonw2.6
lrwxr-xr-x     1 root  wheel     76 Oct  4 19:21 /usr/bin/pythonw2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7
Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Grigorij, I'm retracting my suggestion to modify /usr/bin/python. See updated post above.

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

python is usually a symbolic link to a version of Python. Do a

$ which python

to see where the link is on your path. List all the pythons in this directory:

$ cd dirname_from_above
$ ls -l python*

You may see python, python2, python27, python3, python35, etc.

If "python" is a link to "python2", you can safely remove it an create a new link to "python3"

$ rm python
$ ln -s python3 python

Post back if you have questions.

Updated: Since this question focuses on Mac environment, do NOT try to remove /usr/bin/python

After reading Mac specific Python installation notes, Mac Linux!

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

So your best bet is an alias or a virtual environment using either virtualenv or mkvirtualenv (available through virtualenvwrapper)

Lots of application require python 2, so is there another way to do it without deleting it?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Be aware that some programs may expect "python" to be "python2" and this change may break things.

As an alternative you could set an alias:

alias py=python3

which is much shorter.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

As a third alternative, you can set up a virtual env using virtualenv or mkvirtualenv. In either case you can specify which Python to include using the -p or --python switch to point at the executable you wish to be the "python" in the venv.. This is the route I usually use.

Hey Chris,

you are the BOSS :thumbsup:

Thank you soooooooooo much !

Greets from germany

Grigorij

+1 Chris' answer and comment regarding virtualenv (although I prefer (Ana)conda). I have found virtual environments very helpful managing different versions and package requirements across various projects. Afaik using a virtual environment to change which python version is currently in your PATH won't affect the default version in /usr/bin that the system uses.

Thanks everyone!