python - 在 Python 2(.7.x) 和 Python 3(.5.x) 之间进行选择

标签 python linux scripting

我是一名 Java 开发人员,拥有超过 10 年的经验。

我几个月前开始使用 python,当时我需要创建一个脚本,从 REST 服务中提取数据,然后使用该数据生成报告。 python 是一种多用途语言(脚本、Web 应用程序、REST 服务等),加上非常快的开发速度,这一事实激发了我对这种语言的浓厚兴趣。事实上,这是我在 Linux 世界中使用的唯一语言。

目前我正在尝试将我的 (powershell/shell) 自动化脚本移植到 python。然而,摆在我面前的一个主要挑战是我应该使用哪个版本(2 或 3)的 python?理想情况下我更喜欢 3,因为我相信它比版本 2 有很多改进,我想使用这个版本的所有新开发。然而,我担心的是可能有一些软件包可能还不是 python 3 的版本。这也是 python.org 网站上提到的内容 -

However, there are some key issues that may require you to use Python 2 rather than Python 3.

Firstly, if you're deploying to an environment you don't control, that may impose a specific version, rather than allowing you a free selection from the available versions. Secondly, if you want to use a specific third party package or utility that doesn't yet have a released version that is compatible with Python 3, and porting that package is a non-trivial task, you may choose to use Python 2 in order to retain access to that package.

One popular module that don't yet support Python 3 is Twisted (for networking and other applications). Most actively maintained libraries have people working on 3.x support. For some libraries, it's more of a priority than others: Twisted, for example, is mostly focused on production servers, where supporting older versions of Python is important, let alone supporting a new version that includes major changes to the language. (Twisted is a prime example of a major package where porting to 3.x is far from trivial.)

所以我不想遇到这样一种情况,即有一个我认为对我的自动化脚本非常有用但没有 python 3 版本的包。

最佳答案

使用 python 3。不支持 python 3 的包的数量每天都在减少,并且绝大多数大型/重要的框架已经支持两者。甚至有一些项目完全放弃了 python 2,尽管这些项目往往并不大(因为企业惯性往往会阻碍项目)。

今天在 python 2 上开始一个新项目,尤其是作为初学者,在我看来,比遇到不支持 python 3 的包更痛苦。

考虑到 python 的多功能性和充满活力的 python 社区的规模,通常有多个包可以解决同一个问题。这意味着即使您找到一个不支持 python 3 的项目,通常也可以找到一个支持 python 3 的类似项目。

一旦你对 python 3 有了足够的信心,并且你确实遇到了一个只支持 python 2 的包,你总是有源代码并且可以开始回馈补丁! :D

关于python - 在 Python 2(.7.x) 和 Python 3(.5.x) 之间进行选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35859441/

相关文章:

linux - 我如何检查两个文件夹在 linux 中是否相同

javascript - 您是否可以在 HTML 文本区域/文本字段中有一个脚本并执行它?

python - 用 python 编写脚本语言

linux - wxPython中的非轮询文件监控

python - 根据特定日期设置 matplotlib 网格刻度

python - 如何在另一个函数中调用函数?

python - 替换问题 - Pandas 数据框

linux - cAdvisor 不显示所有容器的正常运行时间(Prometheus+cAdvisor+Grafana)

linux - 如果失败则嵌套 - 伪代码示例

python - 如何正确训练 tf.keras.layers.BatchNormalization : Is there still a tf. GraphKeys.UPDATE_OPS 依赖项?