Bash 别名 --> Python 2.7 到 Python 3.3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27789972/
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
Bash alias --> Python 2.7 to Python 3.3
提问by laurenll
I am trying to make Python 3.4.2 the default in Linux (currently it is 2.7.6). I am not very knowledgeable on this stuff, but I have read in several places online that you can simply put an alias in the ~/.bashrc
or ~/.bash_aliases
file like this:
我正在尝试将 Python 3.4.2 设为 Linux 中的默认值(目前为 2.7.6)。我对这方面的知识不是很了解,但是我在网上的几个地方读到过,您可以简单地在~/.bashrc
或~/.bash_aliases
文件中放置一个别名,如下所示:
alias python='python3'
I don't have either the ~/.bashrc
or ~/.bash_aliases
file . . . I am assuming you can just create them. I have done that, but the alias doesn't seem to be working. Am I missing something? Do you need the shebang at the beginning of the file? I have tried it both ways.
我没有~/.bashrc
或~/.bash_aliases
文件。. . 我假设您可以创建它们。我已经这样做了,但别名似乎不起作用。我错过了什么吗?你需要文件开头的shebang吗?我已经尝试了两种方法。
Thanks for any help you can give!
谢谢你提供的所有帮助!
采纳答案by Se?kin Sava???
DON'T DO IT!
不要这样做!
Some linux utilities depend on python2.x
currently. It will probably break your system if you make that change since python3.x
is not backward compatible with python2.x
. Unless you are fully aware of the consequences, don't do it!
一些 linux 实用程序依赖于python2.x
当前。如果您进行更改,它可能会破坏您的系统,因为python3.x
它不向后兼容python2.x
. 除非你完全意识到后果,否则不要这样做!
Similar question is asked here : https://askubuntu.com/questions/103469/how-do-i-change-my-pythonpath-to-make-3-2-my-default-python-instead-of-2-7-2
类似的问题在这里问:https: //askubuntu.com/questions/103469/how-do-i-change-my-pythonpath-to-make-3-2-my-default-python-instead-of-2- 7-2
回答by Kwuite
In a bash file the following will not work:
在 bash 文件中,以下内容不起作用:
alias python='python3'
别名 python='python3'
The alias syntax is not available in bash/sh script execution. In order to execute python commands with bash that work on both python and python3. I wrote a function that checks if python3 or python is available and then pass on the function argument to that local installation of python.
别名语法在 bash/sh 脚本执行中不可用。为了使用 bash 执行 python 命令,该命令适用于 python 和 python3。我编写了一个函数来检查 python3 或 python 是否可用,然后将函数参数传递给 python 的本地安装。
The example code is taken from a script that runs on machines that sometimes do not have python but do have python3 installed.
示例代码取自在有时没有 python 但安装了 python3 的机器上运行的脚本。
This code was tested on Ubuntu 18 and 16 (Windows Subsystem Linux 2).
此代码在 Ubuntu 18 和 16(Windows 子系统 Linux 2)上进行了测试。
#!/bin/bash
function local_python() {
_python=$(which python)
_python3=$(which python3)
python=${_python3:-_python}
echo $($python -c "")
}
curl "http://www.geoplugin.net/json.gp" \
-X GET \
-H "Accept: application/json" |
local_python "import sys, json; print(json.loads(sys.stdin.read())['geoplugin_request'])"
The above code can be copied and pasted into an example.sh file and should (as long as url is available) return with your IP address.
可以将上述代码复制并粘贴到 example.sh 文件中,并且应该(只要 url 可用)返回您的 IP 地址。