Skip to content Skip to sidebar Skip to footer

How To Run Code Using Both 3.2 And 2.7 Python Interpreter In Same Project?

I have two Python classes written in two different files. One is written in Python 2.7 and the other written in Python 3.2. One class is used inside the other. Is it possible to r

Solution 1:

I don't believe it is possible for them to be running in the same process, that is you will have to choose one or the other. Python3 and Python2 bytecode are not compatible with each other, which you can confirm by attempting to run Python2 bytecode in Python3:

% cat > test.py
a = 1
% python2.6 -m compileall .% python2.6 test.pyc% python3.1 test.pyc
RuntimeError: Bad magic number in .pyc file

Try something more complicated to be sure. Compile test.py using Python2 and then remove the .py file to make sure it isn't recompiled by Python3. Then, attempt to import the .pyc bytecode into a Python3 interpreter.

% python2.6 -m compileall .% rm test.py% cat > test2.py
import test
print(test.a)
% python2.6 test2.py
1
% python3.1 test2.py
Traceback (most recent call last):
  File "test2.py", line 1, in <module>
    import test
ImportError: Bad magic number in test.pyc

Solution 2:

Its easy as 1-2-3. Hope you have both Python 2.7 and Python 3.X.X installed in your system. So in your command prompt just type 1) py -2 //for programs written in Python 2 2) py -3 //for programs in Python 3

Thanks

Post a Comment for "How To Run Code Using Both 3.2 And 2.7 Python Interpreter In Same Project?"