在我的电脑上
~$ python -V
Python 3.2.1
Run Code Online (Sandbox Code Playgroud)
但是当我运行一些python程序时,我遇到了问题.我猜是(或者至少我想试试这个)有一些向后兼容性问题,我想运行那些python脚本
python2 2.7.2-2
Run Code Online (Sandbox Code Playgroud)
它也安装在我的系统上,但我不知道如何将其作为(临时)默认python.python脚本以
#!/usr/bin/env python
Run Code Online (Sandbox Code Playgroud)
我正在使用arch linux.
Mik*_*ike 71
你可以使用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
Run Code Online (Sandbox Code Playgroud)
请享用!
Dou*_*son 13
mkdir ~/bin
PATH=~/bin:$PATH
ln -s /usr/bin/python2 ~/bin/python
Run Code Online (Sandbox Code Playgroud)
停止使用python2,exit或rm ~/bin/python.
o1i*_*ver 12
只需使用python2.7或python2而不是python调用脚本.
所以:
python2 myscript.py
Run Code Online (Sandbox Code Playgroud)
代替:
python myscript.py
Run Code Online (Sandbox Code Playgroud)
您可以做的是替换当前链接到python3的/ usr/bin中的符号链接"python",其中包含指向所需python2/2.x可执行文件的链接.然后你可以像使用python 3一样调用它.
你不想要一个"临时默认的Python"
您希望2.7脚本开始
/usr/bin/env python2.7
Run Code Online (Sandbox Code Playgroud)
并且您希望开始使用3.2脚本
/usr/bin/env python3.2
Run Code Online (Sandbox Code Playgroud)
"默认"Python真的没用.而"临时违约"的想法只是一条绝对混乱的道路.
记得.
显式优于隐式.
你可以使用alias python="/usr/bin/python2.7":
bash-3.2$ alias
bash-3.2$ python
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> ^D
bash-3.2$ alias python="/usr/bin/python3.3"
bash-3.2$ python
Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 16 2013, 23:39:35)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Run Code Online (Sandbox Code Playgroud)
小智 5
如果你有virtualenv的一些问题,
你可以用它:
sudo ln -sf python2 /usr/bin/python
Run Code Online (Sandbox Code Playgroud)
和
sudo ln -sf python3 /usr/bin/python
Run Code Online (Sandbox Code Playgroud)