我刚切换到Python3,但无法将其设置为python的默认版本 [英] I just switched over to Python3 and I can't set it to be my default version of python

查看:216
本文介绍了我刚切换到Python3,但无法将其设置为python的默认版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为我的问题寻找解决方案时,所有建议都指向仅在$PATH变量前添加该版本Python的路径,但这无济于事.

When searching for a solution for my problem, all advice points towards just prepending my $PATH variable with the path to that version of Python, but that doesn't help.

这是我的.bash_profile:

Here's my .bash_profile:

PATH="/usr/local/mysql/bin:/bin:/usr/bin:/usr/local/bin:~/bin"
export PATH="~/bin/python/anaconda:/opt/local/bin:/opt/local/sbin:$PATH"
export PATH="/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4:$PATH"

PYTHONPATH="/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4:${PYTHONPATH}


$ which python
/usr/bin/python


>>> import sys
>>> for p in sys.path:
...     print(p)
...

/Library/Frameworks/Python.framework/Versions/3.4/lib/python34.zip
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages

推荐答案

几乎所有* nix系统(例如MacOSX,大多数Linux发行版)都希望python引用Python 2; python3到Python3.如果更改此设置,则可能会严重损坏.因此,即使 PEP 394 讨论了python,并且所有Python 2脚本在shebang中都使用python2,但这还不是事实.许多程序期望python代表python2.

Almost all *nix systems like MacOSX, most Linux distributions, expect python to refer to Python 2; python3 to Python 3. If you change this, something might break badly. Thus even though the PEP 394 talks about the "default" python distribution for python, and that all Python 2 scripts would use python2 in shebang, it is not the fact yet. Many programs expect python to stand for python2.

此外,即使在这样的系统中,您仍应在脚本前加上python3前缀,并使用该命令显式运行Python 3程序.只需在脚本前加上类似

Furthermore even in such a system you still should prefix your scripts with python3 and explicitly run your Python 3 programs with that command. Just prefix your scripts with something like

#!/usr/bin/env python3

它应该可以工作.

如果您对执行时每次打开python 2.7感到困惑,请在.bashrc中做一个别名:

If you are bugged about python 2.7 opening whenever you execute python, do an alias in your .bashrc:

alias python=python3


我也不知道您对PYTHON_PATH所做的一切是否明智; python解释器将知道在没有这种黑客的情况下可以在哪里寻找自己的库.


Also I am not sure if whatever you are doing with PYTHON_PATH is wise; the python interpreter will know where to look for its own libraries without this hackery.

这篇关于我刚切换到Python3,但无法将其设置为python的默认版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆