3

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 run both of them so that one class will use the 3.2 interpreter and the other will use the 2.7 interpreter?

For instance in the terminal, can I just run the following command?

python3.2 firstClass.py

Any suggestions?

Thanks

1
  • Same "project" or same "process" at run time? "One class is used inside the other" doesn't mean anything. One class can reference another; that's pretty common. Since the syntax is different between Python 3.2 and 2.7, you'll have to provide a lot more details. Commented May 13, 2011 at 0:12

2 Answers 2

5

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.