unix 如何选择从终端运行哪个版本的python?

kgqe7b3p  于 2023-03-29  发布在  Unix
关注(0)|答案(6)|浏览(147)

我的电脑上有几个不同版本的python。当我在提示符中输入python时,我如何选择从我的终端运行哪一个?

kcwpcxri

kcwpcxri1#

使用which查看您的python命令所在的位置。然后使用ls -l找出它的真正位置。然后链接您想要的版本。请注意,其他安装的版本通常都可以通过各自的名称获得。

$ which python
/usr/bin/python
$ ls -l /usr/bin/python
lrwxrwxrwx 1 root root 9 Jun 18  2013 /usr/bin/python -> python2.7
$ ls /usr/bin/python*
/usr/bin/python   /usr/bin/python2.7         /usr/bin/python2-config
/usr/bin/python2  /usr/bin/python2.7-config  /usr/bin/python-config
$ sudo ln -sf /usr/bin/python2 /usr/bin/python

请注意,这会改变您计算机上所有用户的所有程序可能使用的Python版本**!如果您只想为自己更改它。您可以通过添加alias python='/usr/bin/python2'行(用您想要的版本替换python2)将其别名添加到Linux中的~/.bashrc或Mac中的~/.bash_profile。(在这种情况下,您需要重新启动终端会话。)

whlutmcx

whlutmcx2#

对于每个python版本,你都应该有多个可执行文件。例如,如果我输入python并点击tab,我会看到:

$ python
python             python2.5-config   python2.7-config   python3.3          python3.3m-config  pythonw2.7         pythonw3.3-32      
python-config      python2.6          python3            python3.3-32       pythonw            pythonw3           
python2            python2.6-config   python3-32         python3.3-config   pythonw2.5         pythonw3-32        
python2.5          python2.7          python3-config     python3.3m         pythonw2.6         pythonw3.3

例如,如果我想要python 2.5版本,我运行python2.5
另外,看看virtual environments--用它管理和切换多个python环境要容易得多。
另见:

inn6fuwd

inn6fuwd3#

py -3或py -2等来选择版本。甚至32/64位版本也可以区分:

py -2 
py -3.7-32
py -3.7-64

参见https://docs.python.org/3/installing/#work-with-multiple-versions-of-python-installed-in-parallel

qrjkbowd

qrjkbowd4#

要选择在终端中键入'python'时运行的python版本,您可能需要尝试使用别名。
例如:

alias python='python2.7'

当你在终端中输入'python'时,会使python2.7执行。

sbtkgmzw

sbtkgmzw5#

尝试envirius (universal virtual environments manager),它允许编译任何版本的python。此外,它允许创建混合语言的环境。

ogsagwnx

ogsagwnx6#

一个选项是update-alternatives

$ sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                 Priority   Status
------------------------------------------------------------
  0            /usr/bin/python3.11   2         auto mode
* 1            /usr/bin/python3.10   1         manual mode
  2            /usr/bin/python3.11   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 0
update-alternatives: using /usr/bin/python3.11 to provide /usr/bin/python3 (python3) in auto mode
$ python3 -V
Python 3.11.0rc1
$ sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                 Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.11   2         auto mode
  1            /usr/bin/python3.10   1         manual mode
  2            /usr/bin/python3.11   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/python3.10 to provide /usr/bin/python3 (python3) in manual mode
$ python3 -V
Python 3.10.6

相关问题