为 python3 创建别名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35435517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:26:34  来源:igfitidea点击:

creating an alias for python3

pythonbash

提问by Cindy

I have python2.6.6 installed in my system. Now I am trying to use python3 while keeping all the python2.6.6 untouched in /usr/bin/. I am able to install python3 in /opt/python35/bin/. By adding export PATH=/opt/python35/bin/:$PATHto the ~/.bashrc file, I am able to access python3 anywhere in my console.

我的系统中安装了 python2.6.6。现在我正在尝试使用 python3,同时在 /usr/bin/ 中保持所有 python2.6.6 不变。我可以在 /opt/python35/bin/ 中安装 python3。通过添加export PATH=/opt/python35/bin/:$PATH到 ~/.bashrc 文件,我可以在控制台的任何地方访问 python3。

My question is: how could I set an alias (python) for python3 so that, whenever I issue command "python", python3 in /opt/python35/bin/ could be used? I simply couldn't remove python2.6.6 in my system due to some already installed programs in my system.

我的问题是:如何为 python3 设置别名(python),以便在我发出命令“python”时,可以使用 /opt/python35/bin/ 中的 python3?由于我的系统中已经安装了一些程序,我根本无法删除系统中的 python2.6.6。

My current approach is to add a line in the ~/.bashrc file, alias python = "/opt/python35/bin/python3"or simply alias python = "python3". However, when I reload this ~/.bashrc file, I got the following error:

我目前的方法是在 ~/.bashrc 文件中添加一行,alias python = "/opt/python35/bin/python3"或者简单地添加alias python = "python3". 但是,当我重新加载这个 ~/.bashrc 文件时,出现以下错误:

$ source ~/.bash_profile
bash: alias: python: not found
bash: alias: =: not found
bash: alias: /opt/python35/bin/python3: not found

Does anybody know where my problem is? Thanks in advance!

有人知道我的问题出在哪里吗?提前致谢!

回答by KIDJourney

in your .bashrc

在你的 .bashrc

add

添加

alias python='python3'

In bash script , you can't insert space beside =.

在 bash script 中,您不能在=.

回答by neuhaus

Spaces are not allowed next to the equal sign when declaring aliases.

声明别名时,等号旁边不允许有空格。

Use

alias python=python3

and it should work.

它应该工作。