问题
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):
- Does anyone know where the Python 3.4.3 interpreter was installed?
- 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?
回答1:
Try typing python3
instead of just python
.
回答2:
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.
回答3:
In order to use Python 3.x, type python3
instead of python
.
回答4:
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
回答5:
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.
回答6:
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'
.
回答7:
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
i.e.
$ 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)
来源:https://stackoverflow.com/questions/28921333/installed-python-3-on-mac-os-x-but-its-still-python-2-7