简体   繁体   English

Python 或 Python3。 有什么区别?

[英]Python or Python3. What is the difference?

What is the difference between following commands:以下命令有什么区别:

python setup.py

and

python3 setup.py
  • What if I only have python3.6 installed?如果我只安装了 python3.6 怎么办? python and python3 would do the same thing? pythonpython3会做同样的事情吗?
  • Does it make difference only when I have different versions of python installed?只有当我安装了不同版本的 python 时,它才有区别吗? If so, which version will be used with python setup.py ?如果是这样,哪个版本将与python setup.py一起使用?

Yes, it will make difference if you have different versions of python installed.是的,如果你安装了不同版本的 python,它会有所不同。

This depends on the entries in on the PATH environment variable.这取决于 PATH 环境变量中的条目。 Suppose you have two python installations, 2.7 and 3.8, now you have installed 2.7 before 3.8, and both were added to PATH, so when you type python , 2.7 interpreter launches.假设您有两个 python 安装,2.7 和 3.8,现在您已经在 3.8 之前安装了 2.7,并且都已添加到 PATH,因此当您键入python ,2.7 解释器启动。 If you have done vice versa, then 3.8 would launch.如果反之亦然,则 3.8 将启动。 You can type where python to determine location.您可以输入where python来确定位置。

Also one thing is that there is a launcher named py , just type py -3.8 3.8 interpreter will launch and same on py -2.7还有一件事是有一个名为py的启动器,只需键入py -3.8 3.8 解释器将启动,在py -2.7

The only difference is how the you aliased each version.唯一的区别是您如何为每个版本设置别名。 you can run which python to show which python is running using the python alias.您可以使用python别名运行which python以显示哪个 python 正在运行。 Usually this will be the same python in the python-path which packages installed with pip will be installed to.通常,这将与安装了 pip 的软件包安装到的 python-path 中的 python 相同。

There is no single correct answer here, but we can offer some common observations.这里没有单一的正确答案,但我们可以提供一些常见的观察结果。

  • Some Linux distributions decided during the transition from Python 2 to Python 3 that python should always refer to Python 2, and the command to run Python 3 would be python3 with a 3 at the end.在从 Python 2 过渡到 Python 3 的过程中,一些 Linux 发行版决定python应该始终指的是 Python 2,并且运行 Python 3 的命令将是python3 ,末尾带有 3。 Now that Python 2 is becoming obsolete, this is being relaxed in some distros (ie now that they no longer ship Python 2 at all, the python command can be allowed to point to Python 3, too), but this is going to continue to exist in some form for some time.现在 Python 2 已经过时了,这在一些发行版中被放宽了(即现在他们根本不再提供 Python 2,也可以允许python命令指向 Python 3),但这将继续以某种形式存在一段时间。
  • Debian-based Linux distros have a mechanism called update-alternatives which lets you define which version of python exactly will be linked to the system-wide standard /usr/bin/python .基于 Debian 的 Linux 发行版有一种称为update-alternatives的机制,它可以让您定义哪个版本的python将链接到系统范围的标准/usr/bin/python There is similarly a set of alternatives for /usr/bin/python3 . /usr/bin/python3也有类似的一组替代方案。
  • If you have manually installed Python 3 somewhere, what works depends on where it was installed and whether that location is in your PATH .如果您已经在某处手动安装了 Python 3,那么什么工作取决于它的安装位置以及该位置是否在您的PATH A common arrangement is to have Python 3.14.159 somewhere like /opt/python-3.14.159/bin/python3.14.159 and then rely on users who want to use this to create a symlink or alias to this binary so that they can simply call it python (or python3 , or obviously also eg shirley if they prefer that)一个常见的结构是具有Python的3.14.159地方,比如/opt/python-3.14.159/bin/python3.14.159然后依靠谁想要使用它来创建一个符号链接或别名这个二进制文件,使他们可以简单地用户称其为python (或python3 ,或者如果他们喜欢,显然也可以例如shirley
  • If you have an interactive alias, function, or personal shell script in your PATH with either of these names, obviously that overrides any system-wide setting, and could basically do anything at all, including but not limited to running a specific version of Python, ideally with all the command line arguments intact and correctly quoted ( /path/to/system-wide/default/python "$@" ) but obviously with no guarantees of any sort.如果您的PATH有一个交互式别名、函数或个人 shell 脚本,并且使用这些名称中的任何一个,显然这会覆盖任何系统范围的设置,并且基本上可以做任何事情,包括但不限于运行特定版本的 Python ,理想情况下,所有命令行参数完好无损并正确引用( /path/to/system-wide/default/python "$@" ),但显然没有任何形式的保证。
  • On Windows, where many of these facilities don't exist, a common arrangement is for Python to be installed in C:\\python3.14.159\\bin\\Python.exe ;在 Windows 上,其中许多工具都不存在,通常的安排是将 Python 安装在C:\\python3.14.159\\bin\\Python.exe you then have to have C:\\python3.14.159\\bin in your PATH for the python command to work.然后你必须在你的PATHC:\\python3.14.159\\bin才能让python命令工作。 The installer lets you install the package anywhere, but if you just go with the defaults, this is what you commonly end up with.安装程序允许您在任何地方安装软件包,但如果您只使用默认值,这就是您通常会得到的结果。 Because of the cumbersomeness of Windows, the standard install also includes a command py which works around some of the rigidity of the platform to let you flexibly indicate which Python version exactly to run.由于 Windows 的繁琐,标准安装还包括一个命令py ,它可以解决平台的一些刚性问题,让您灵活指示确切运行哪个 Python 版本。 There is usually not a separate python3 command, though users are of course free to create a CMD file with that name to run Python (or play a video of Rick Astley performing his biggest hit if they so prefer).通常没有单独的python3命令,尽管用户当然可以自由创建一个具有该名称的CMD文件来运行 Python(或者如果他们愿意,可以播放 Rick Astley 表演他最热门歌曲的视频)。

To help us help you debug questions about your installation, you will commonly need to tell us exactly what you installed where and how, and show us the value of your PATH .为了帮助我们帮助您调试有关安装的问题,您通常需要准确地告诉我们您安装的位置和方式,并向我们显示PATH的值。 If you have relevant functions, aliases, or personal shell scripts which shadow the system-wide default ones, show them too.如果您有相关的函数、别名或个人 shell 脚本,它们会影响系统范围的默认值,请将它们也显示出来。 On Unix-based platforms, the type and command commands can be used to show what will be executed (many guidances will tell you to use which or whereis , but don't; now we have to guess which non-standard command you are using, and where it is going to look).在基于 Unix 的平台上, typecommand命令可用于显示将执行的命令(许多指南会告诉您使用whichwhereis ,但不要;现在我们必须猜测您使用的是哪个非标准命令,以及它要去哪里)。 The Windows command whereis provides roughly the same functionality. Windows 命令whereis提供大致相同的功能。


Don't call me Shirley, please.请不要叫我雪莉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM