python 为什么有些模块是在 win 而不是 linux 上定义的

python why some modules are defined on win and not on linux

我开始写python脚本

我使用 pyCharm 或 VS 2010 作为编辑器

然后我将脚本从 win 复制到我的 Linux 机器

我的 Linux 机器上有 python 版本 2.4.3 在我的 win 机器上,我安装了 python 版本 3.4

我看到 Linux 机器中存在一些模块 作为

    import commands

并且在我的 win 机器上安装的 python 上不存在,尽管我的 win 版本上的 python 版本更高

所以我的问题是在我的 win 机器上构建所有缺少的模块的建议是什么

因为直接在我的 linux 机器 VIA vi 上写脚本的想法很糟糕

如码头所述,'commands' 自版本 2.6 起已弃用。
https://docs.python.org/2/library/commands.html#module-commands

他们建议改用 'subprocess' 模块..
https://docs.python.org/2/library/subprocess.html#module-subprocess

如果你的问题是"can you assume that any module available on one computer is available on another",答案显然是否定的。一台计算机上可能安装了第三方库,但另一台计算机上没有。即使您的问题是 "can you assume that any builtin module available on one computer is available on another",答案仍然是否定的,因为有特定于平台的模块。

最后,即使所有相同的模块都可用,也不意味着您的代码可以工作。您不能指望为 Python 3 编写的代码可以在 Python 2 上运行。您可以编写适用于 Python 两个版本的代码,但除了确保仅使用两个系统上可用的库之外,您还必须为此做好计划并仔细编写代码。