/ (一時的な)デフォルトのpythonとしてのpython 3の代わりに/ python 2? - python

python 2の代わりにpython 3(一時的な)デフォルトのpythonとして? - python

私のコンピュータ上で

~$ python -V
Python 3.2.1

しかし私はいくつかのpythonプログラムを実行するとき私は問題に巻き込まれます。私は、(あるいは少なくとも私はこれを試してみたいと思いますが)後方互換性の問題がいくつかあるので、これらのPythonスクリプトを次のように実行したいと思います

 python2 2.7.2-2

これは私のシステムにもインストールされていますが、それを(一時的な)デフォルトのpythonにする方法がわかりません。 Pythonスクリプトは

 #!/usr/bin/env python

そして私はarch linuxを使っています。

回答:

回答№1の場合は62

あなたは使うことができます 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

楽しい!


回答№2については9

ただpythonの代わりにpython2.7やpython2のようなものを使ってスクリプトを呼び出すだけです。

そう:

python2 myscript.py

の代わりに:

python myscript.py

代わりにできることは/ usr / bin内のシンボリックリンク "python"。現在はpython3にリンクしており、必要なpython2 / 2.x実行ファイルへのリンクがあります。それから、python 3と同じようにそれを呼び出すことができます。


回答№3の9

「一時的なデフォルトのPython」は必要ありません。

あなたは2.7スクリプトを始めたいと思うでしょう

/usr/bin/env python2.7

そして、あなたは3.2のスクリプトがで始まることを望みます

/usr/bin/env python3.2

「デフォルト」のPythonには実際には意味がありません。

覚えておいてください。

ExplicitはImplicitよりも優れています。


回答№4のための8

あなたは使うことができます 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.
>>>

回答№5については8
mkdir ~/bin
PATH=~/bin:$PATH
ln -s /usr/bin/python2 ~/bin/python

python2の使用をやめるには、 exit または rm ~/bin/python.


答え№6のための5

virtualenvに問題がある場合は、

あなたはそれを使用することができます:

sudo ln -sf python2 /usr/bin/python

そして

sudo ln -sf python3 /usr/bin/python

答え№7の2

直接シェルではなく、pythonコマンドを使用してスクリプトを起動します。例えば。

  python2 /usr/bin/command

私の知る限りでは、これはenvインタプリタの行が悪いスクリプトを回避するための推奨方法です。