How to run code using both Python 3.2 and 2.7 translators in the same project?

I have two Python classes written in two different files. One is written in Python 2.7, and the other is written in Python 3.2. One class is used inside another.

Is it possible to run both of them so that one class uses the interpreter 3.2, and the other uses the interpreter 2.7?

For example, in the terminal, can I run the following command?

python3.2 firstClass.py

Any suggestions?

thank

+3
source share
2 answers

I do not believe that they can work in one process, that is, you will have to choose one or the other. Python3 and Python2 bytecode are incompatible with each other, which you can confirm by trying 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

- , . test.py Python2, .py, , Python3. - .pyc Python3.

% 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
+5

, 1-2-3. , Python 2.7, Python 3.X.X. 1) py -2// , Python 2 2) py -3// Python 3

0

All Articles