gpt4 book ai didi

python - 如何在同一项目中同时使用 3.2 和 2.7 Python 解释器运行代码?

转载 作者:太空宇宙 更新时间:2023-11-03 14:33:45 26 4
gpt4 key购买 nike

我在两个不同的文件中编写了两个 Python 类。一个是用 Python 2.7 编写的,另一个是用 Python 3.2 编写的。一个类在另一个类中使用。

是否可以同时运行它们以便一个类使用 3.2 解释器而另一个类使用 2.7 解释器?

例如在终端中,我可以只运行以下命令吗?

python3.2 firstClass.py

有什么建议吗?

谢谢

最佳答案

我认为它们不可能在同一个进程中运行,也就是说您必须选择其中之一。 Python3 和 Python2 字节码彼此不兼容,您可以通过尝试在 Python3 中运行 Python2 字节码来确认:

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

尝试一些更复杂的东西来确定。使用 Python2 编译 test.py,然后删除 .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

关于python - 如何在同一项目中同时使用 3.2 和 2.7 Python 解释器运行代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5985688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com