我的 Mac 默认安装了 Python 2.7,但我想使用 Python 3.6.1。
如何在终端中更改 Python 版本 (在 Mac OS 上)?
请清晰地解释,并不要提供第三方版本管理建议。
最简单的方法是将一个别名添加到python3
,使其始终指向本地安装的python
。在您的$HOME
目录中的.bash_profile
文件的最后添加此行:
alias python="python3"
这样做会使得更改反映在每个打开的交互式 shell 上。
virtualenv
。 - Inian正如Inian所建议的那样,你应该将python别名指向python 3。这很容易做到,而且非常容易切换回来,我个人也设置了p2=python2和p3=python3的别名以节省按键次数。
在此处阅读更多信息:如何创建Bash别名?
以下是针对Python执行此操作的示例:
alias python=python3
就像这样:
$ python --version
Python 2.7.6
$ python3 --version
Python 3.4.3
$ alias python=python3
$ python --version
Python 3.4.3
查看原始内容请点击: https://askubuntu.com/questions/320996/how-to-make-python-program-command-execute-python-3
pyenv
是一个超级常用的第三方版本管理器(18k星,1.6k分支),正是我在提问时寻找的东西。
编辑:我现在已经使用它好几年了。运行得非常好。
安装{{link1:pyenv
}}。
$ pyenv install --list
Available versions:
2.1.3
[...]
3.8.1
3.9-dev
activepython-2.7.14
activepython-3.5.4
activepython-3.6.0
anaconda-1.4.0
[... a lot more; including anaconda, miniconda, activepython, ironpython, pypy, stackless, ....]
$ pyenv install 3.8.1
Downloading Python-3.8.1.tar.xz...
-> https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tar.xz
Installing Python-3.8.1...
Installed Python-3.8.1 to /home/moose/.pyenv/versions/3.8.1
$ pyenv versions
* system (set by /home/moose/.pyenv/version)
2.7.16
3.5.7
3.6.9
3.7.4
3.8-dev
$ python --version
Python 2.7.17
$ pip --version
pip 19.3.1 from /home/moose/.local/lib/python3.6/site-packages/pip (python 3.6)
$ mkdir pyenv-experiment && echo "3.8.1" > "pyenv-experiment/.python-version"
$ cd pyenv-experiment
$ python --version
Python 3.8.1
$ pip --version
pip 19.2.3 from /home/moose/.pyenv/versions/3.8.1/lib/python3.8/site-packages/pip (python 3.8)
在运行程序时,您可以指定Python版本:
对于Python 2:
python filename.py
针对Python 3:
python3 filename.py
如果您安装了多个Python版本,可以使用pythonx.x.x
来启动其中任何一个,其中x.x.x
代表您的版本号。
补充Inian的答案(被接受的答案),
python3 -V
alias python="python3.9"
要安装Python的新版本,你可以在MAC上使用homebrew
一旦安装了homebrew,你就可以通过homebrew安装新的Python版本
brew install python@3.9
然后使用以下命令切换到这个新版本
alias python="python3.9"
检查Python版本以确认更改
检查所有已安装的Python版本
brew list | grep python
我已经在MacBook上按照以下步骤进行操作。
我是Python的初学者,正在寻找相同的东西,在终端中,我只需键入python3
,就会出现最新版本。我认为,如果想要使用不同的版本,只需键入即可?可能是错误的,但这就是当我键入python3时显示的内容。
% python3
Python 3.9.2 (v3.9.2:1a79785e3e, Feb 19 2021, 09:06:10)
[Clang 6.0 (clang-600.0.57)] on darwin
以前当我只键入Python时,我会收到这个信息。
% python2.7
WARNING: Python 2.7 is not recommended.
This version is included in macOS for compatibility with legacy software.
Future versions of macOS will not include Python 2.7.
Instead, it is recommended that you transition to using 'python3' from within Terminal.
Python 2.7.16 (default, Jun 5 2020, 22:59:21)
[GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc- on darwin
Type "help", "copyright", "credits" or "license" for more information.
conda create --name py=3.8 python=3.8
:: conda create --name <env_name> python=<version>
conda init powershell
:: conda init <something>
python --version
:: output current version for eg python 2.7
conda activate py=3.8
python --version
:: output python 3.8
alias
命令用于将python
指向python3
,这是最简单的技巧。 - Inian