Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? What's the reason you can't call functions written in python 2 in python 3?

like image 909
Zorf Avatar asked Nov 10 '22 15:11

Zorf


1 Answers

In some cases you could, if the code was in pure Python and written to tbe compatible with both Python 2 and 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. There is nothing that corresponds to the compiled form of Java. If the source code of your Python module doesn't work with the new Python, then your module will not work.

(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.)

I don't understand how the issue of C modules is related here. Python modules written in C have to be recompiled even for different versions of Python 2.

like image 116
BrenBarn Avatar answered Nov 15 '22 07:11

BrenBarn