简体   繁体   English

为什么不能在python 3中导入python 2模块?

[英]Why can't you import python 2 modules in python 3?

Is there any theoretical reason that stops this? 是否有任何理论上的理由阻止了这一点? As far as I know python modules can be written in C? 据我所知,python模块可以用C编写? What's the reason you can't call functions written in python 2 in python 3? 你不能在python 3中调用python 2中编写的函数是什么原因?

In some cases you could, if the code was in pure Python and written to tbe compatible with both Python 2 and Python 3. 在某些情况下,如果代码是纯Python 并且编写为与Python 2和Python 3兼容,则可以。

In many cases you can't, because they're different languages, so code written for one isn't necessarily valid code in the other. 在许多情况下你不能,因为它们是不同的语言,所以为一个编写的代码不一定是另一个有效的代码。

Python is interpreted, not compiled, so the actual source code has to be run whenever you use the module. Python是解释的,而不是编译的,因此每当您使用该模块时都必须运行实际的源代码。 There is nothing that corresponds to the compiled form of Java. 没有任何东西与Java的编译形式相对应。 If the source code of your Python module doesn't work with the new Python, then your module will not work. 如果Python模块的源代码不能与新的Python一起使用,那么您的模块将无法工作。

(There is a bytecode compiled form of Python, but unlike the Java VM it is not a stable, specified target, so the bytecode interpreter might change from one version of Python to another just like the language might. See this question on Programmers StackExchange.) (Python有一个字节码编译形式,但与Java VM不同,它不是一个稳定的指定目标,因此字节码解释器可能会从一个版本的Python改为另一个版本,就像语言可能一样。请参阅Programmers StackExchange上的这个问题 。 )

I don't understand how the issue of C modules is related here. 我不明白C模块的问题在这里是如何相关的。 Python modules written in C have to be recompiled even for different versions of Python 2. 用C语言编写的Python模块即使对于不同版本的Python 2也必须重新编译。

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

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