我怎样才能制作“蟒蛇"?命令在终端,运行 python3 而不是 python2? [英] How can I make the "python" command in terminal, run python3 instead of python2?

查看:31
本文介绍了我怎样才能制作“蟒蛇"?命令在终端,运行 python3 而不是 python2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习 Python 并做了一些搜索,所以如果有人问过和回答过,请原谅我.

I'm just starting to learn Python and did search around a little, so forgive me if this has been asked and answered.

通过命令行/终端运行脚本时,我必须输入python3"才能运行最新版本的 Python.对于 Python 2.X,我只使用python".

When running scripts through the command line/terminal, I have to type "python3" to run the latest version of Python. With Python 2.X I just use "python".

有没有办法只使用python"来运行 Python 3?它可能看起来有点懒惰,但我主要只是好奇它是否可能,或者如果我实际上可以做到的话,它是否会破坏任何不必要的东西.

Is there a way to run Python 3 just using "python"? It may seem a little lazy, but I'm mostly just curious if it is possible or if it will break anything unnecessarily if I could in fact do it.

推荐答案

如果您使用的是 Windows,那么您可以使用 适用于 Windows 的 Python 启动器.

If you're using Windows then you can use the Python Launcher For Windows.

这将允许您使用 py 命令来选择不同的 python 安装,例如:

This will allow you to use the py command to select different python installations such as:

py -2.7 # Runs Python 2.7
py -3.3 # Runs Python 3.3
py -2 # Runs the latest version of Python 2.x (so if you have 2.6 and 2.7 it will run 2.7)

同样,您可以在您的 python 文件中设置一个shebang,如下所示:

Similarly you can set a shebang in your python files as demonstrated below:

#! python3
print('Hello World!')

如果您现在使用 py test.py 运行该文件(我们称其为 test.py),它将自动与 Python 3 一起运行.它使 Python 安装到从行首的shebang使用.

If you now run that file (let's call it test.py) with py test.py it will automatically run with Python 3. It gets the Python installation to use from the shebang at the beginning of the line.

您可能想要的是自定义默认python版本.如果您自己调用 py,这将允许您设置默认操作.

What you probably want is to customise the default python version though. This will allow you to set the default actions if you just call py on it's own.

这篇关于我怎样才能制作“蟒蛇"?命令在终端,运行 python3 而不是 python2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆