简体   繁体   中英

Installed Python 3 on Mac OS X but its still Python 2.7

I am currently running OS X Yosemite (10.10.2) on my MacBook Pro... By default, Apple ships Python 2.7.6 on Yosemite.

Just downloaded and ran this installer for Python 3: python-3.4.3-macosx10.6.pkg

When I opened up my Terminal and typed in python , this is what came up:

Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Question(s):

  1. Does anyone know where the Python 3.4.3 interpreter was installed?
  2. Do I need to uninstall Python 2.7.3 (if so, how do I go about doing this) before setting a global environmental variable such as PYTHON_HOME to the location of the installed Python 3.4.3?

尝试输入python3而不仅仅是python

While @rhombidodecahedron's answer is concise and to-the-point and @Nacho Izquierdo addresses your first question perfectly, my answer aims to answer your second question in some more detail:

One should not uninstall Python 2.7 which comes with Mac OS X; it is supplied by Apple and is needed for applications running on OS X. It is stored in /System/Library/Frameworks/.. . If it is removed, Mac OS X will have to be reinstalled.

Hope that helps! And to reiterate answers given by @rhombidodecahedron and @Nacho Izquierdo, install Python 3.x separately and use python3 if you would like to use that version.

Python 2.7 is the standard, Python 3.x is the future.

What you should not do -

moving default python binary to an unused name

$ sudo mv /usr/bin/python /usr/bin/python2

and then moving the new binary to the default path

$ sudo mv $PATHTOBINARY/python3 /usr/bin/python

What could be done but also shouldn't not be done

Since I use zsh by default, I put the following into the .zshrc file:

$ echo "alias python=/usr/local/bin/python3.7" >> ~/.zshrc

If you are using the default Bash shell, you can append this same text to your .bashrc :

$ echo "alias python=/usr/local/bin/python3.7" >> ~/.bashrc

This will work but it is not the recommended way because making future updates to Python will be difficult. It means we have to manually download the new files since Python doesn't include a command-line way to update.

What is the right way

The basic premise of all Python development is to never use the system Python. You do not want the Mac OS X 'default Python' to be 'python3'.

Usage of pyenv to manage Python environments is recommended.

$ brew install pyenv

$ pyenv install 3.7.3

$ pyenv global 3.7.3

$ pyenv version

Refresh the current terminal and check

$ python -V

It should give Python 3.7.3

This way you are good to go.

For further reference - https://opensource.com/article/19/5/python-3-default-mac

Since I know I'll only use python3, I added these 2 lines to .bash_profile file:

alias python="python3" # to use python3 rather than python2.7
alias idle="idle3" # to use python3 idle rather than 2.7

要使用Python 3.x,请键入python3而不是python

In the version OS X El Capitan, you can find the interpreter in: /opt/local/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4

By dragging this path into the Terminal and pressing enter you will be able to run this version.

To run it faster you can either create an alias by typing in the Terminal: alias python = 'python3.4' .

You can easily do this using pyenv which is a Simple Python Version Management. It allows one to set specific Python versions to run on specific directories or one can change your version before using shell

ie

$ pyenv install 2.7.6
$ pyenv install 2.6.8
$ pyenv local 2.7.6
$ pyenv versions
  system
  2.6.8
* 2.7.6 (set by /home/yyuu/.pyenv/version)

After several hours of linking, unlinking, uninstalling and installing python to get a version callback python 3.8.5 rather than python 2.7 adding these 2 lines to .bash_profile file answered above by Sherif Shendidy is what worked for me.

alias python="python3" # to use python3 rather than python2.7 alias idle="idle3" # to use python3 idle rather than 2.7

I have encountered similar problem on windows as well.I would like to address this issue on windows. If you have already installed python 2 and python 3, Note: While installation make sure you click on "Add to environment variables" or "Add to path".

Check version in cmd: python -V

If it shows 2.x, windows > edit the system environment variables > environment variables > user variables > path click edit You should notice a bunch of directories in some order. Find a directory similar to below: C:\\Users\\some name\\AppData\\Local\\Programs\\Python\\Python3.x\\ click on Move up until this directory is on top. click ok . Now restart the cmd. type python -V you will find 3.x

For windows users, I just deleted the folder containing the python2.7. I just reinstalled the python 3.xx and after that reboot my laptop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM