我正在运行Mountain Lion,基本的默认Python版本是2.7。我下载了Python 3.3,并想将其设置为默认值。
目前:
$ python
version 2.7.5
$ python3.3
version 3.3
我如何设置它,以便每次运行$ python时打开3.3?
我正在运行Mountain Lion,基本的默认Python版本是2.7。我下载了Python 3.3,并想将其设置为默认值。
目前:
$ python
version 2.7.5
$ python3.3
version 3.3
我如何设置它,以便每次运行$ python时打开3.3?
当前回答
如果您正在使用virtualenvwrapper,您可以使用哪个virtualenvwrapper.sh定位它,然后使用vim或任何其他编辑器打开它,然后更改以下内容
# Locate the global Python where virtualenvwrapper is installed.
if [ "${VIRTUALENVWRAPPER_PYTHON:-}" = "" ]
then
VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
fi
将一行VIRTUALENVWRAPPER_PYTHON="$(command \which python)"改为VIRTUALENVWRAPPER_PYTHON="$(command \which python3)"。
其他回答
在系统范围内更改默认的python可执行文件版本可能会破坏一些依赖于python2的应用程序。
但是,您可以在大多数shell中使用别名命令,因为macOS中的默认shell (bash在10.14及以下;10.15中的ZSH)共享类似的语法。你可以把 别名python = ' python3 ' 在你的~/。Profile,然后source ~/。在您的~/。Bash_profile和/或你的~/。Zsh_profile,如下所示:
[ -e ~/.profile ] && . ~/.profile
这样,您的别名将跨shell工作。
这样,python命令现在调用python3。如果你想偶尔调用“原始的”python(指python2),你可以使用python命令,这将保持别名不变,并在所有shell中工作。
如果你经常启动解释器(我这样做),你也可以创建更多的别名来添加,即:
alias 2='python2'
alias 3='python3'
提示:对于脚本,不要像下面这样使用shebang:
#!/usr/bin/env python
use:
#!/usr/bin/env python3
这样,系统将使用python3来运行python可执行文件。
如果您正在使用virtualenvwrapper,您可以使用哪个virtualenvwrapper.sh定位它,然后使用vim或任何其他编辑器打开它,然后更改以下内容
# Locate the global Python where virtualenvwrapper is installed.
if [ "${VIRTUALENVWRAPPER_PYTHON:-}" = "" ]
then
VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
fi
将一行VIRTUALENVWRAPPER_PYTHON="$(command \which python)"改为VIRTUALENVWRAPPER_PYTHON="$(command \which python3)"。
$ sudo ln -s -f $(which python3) $(which python)
完成了。
我认为当你安装python时,它会把导出路径语句放到你的~/中。bash_profile文件。所以如果你不打算再使用python2,你可以从那里删除这个语句。如上所述的别名也是一种很好的方法。
下面是如何从~/.bash_profile中删除引用 - vim ./.bash_profile -删除引用(也就是:export PATH="/Users/bla/anaconda:$PATH") - save并退出 - source ./。Bash_profile保存更改
进入终端类型:
alias python=python3.x
这将把默认的python设置为python3.x