如何将python 2设为临时默认python而不是python 3?
在我的电脑上
~$ python -V
Python 3.2.1
但是当我运行一些Python程序时遇到了问题。我猜测(或者说我想尝试一下)可能是有一些向后兼容性的问题,我想用
python2 2.7.2-2
来运行这些Python脚本,这个版本也已经安装在我的系统上,但我不知道怎么把它设置为(临时)默认的Python。Python脚本是以
#!/usr/bin/env python
开头的,我使用的是Arch Linux。
9 个回答
11
你不想要一个“临时的默认Python”
你想要的是从2.7版本的脚本开始
/usr/bin/env python2.7
而且你希望3.2版本的脚本也能开始
/usr/bin/env python3.2
其实没有必要有一个“默认”的Python。而“临时默认”的想法只会让事情变得更加混乱。
记住这一点。
明确比含糊要好。
21
mkdir ~/bin
PATH=~/bin:$PATH
ln -s /usr/bin/python2 ~/bin/python
要停止使用python2,可以输入 exit
或者 rm ~/bin/python
。
85
你可以使用 virtualenv 这个工具。
# Use this to create your temporary python "install"
# (Assuming that is the correct path to the python interpreter you want to use.)
virtualenv -p /usr/bin/python2.7 --distribute temp-python
# Type this command when you want to use your temporary python.
# While you are using your temporary python you will also have access to a temporary pip,
# which will keep all packages installed with it separate from your main python install.
# A shorter version of this command would be ". temp-python/bin/activate"
source temp-python/bin/activate
# When you no longer wish to use you temporary python type
deactivate
祝你玩得开心!