简体   繁体   English

我们可以在python 3.5下运行旧版python 2.7代码吗?

[英]Can we run legacy python 2.7 code under python 3.5?

I'd like to upgrade to python 3.5, but I use legacy python 2.7 packages. 我想升级到python 3.5,但我使用的是旧版python 2.7软件包。 Is it easy to run legacy packages in python 3.5? 在python 3.5中运行旧版软件包是否容易? I have been under the impression that this isn't easy, but I did a few searches to see if I'm wrong and didn't come up with much. 我一直觉得这并不容易,但是我进行了几次搜索以查看我是否错了,并没有提出太多建议。

I would expect there to be a multiprocessing package that allows standardized hand-offs between 3.5 code and 2.7 packages, allowing them to run independently under their own environments, but being somewhat seamless to the developer. 我希望有一个多处理程序包,该程序包允许在3.5代码和2.7程序包之间进行标准化的切换,从而使它们可以在自己的环境中独立运行,但对开发人员而言却是无缝的。

I'm not talking about converting my own code to 3.5, I'm talking about libraries that I use that won't be updated for or by me. 我不是在谈论将自己的代码转换为3.5,而是在谈论我使用的不会为我自己更新的库。

If you used the newer syntax supported by 2.7, eg around exceptions, and/or, better yet, worked with new features imported from __future__ , you'll have much easier time converting your code to Python 3 (up to no changes at all). 如果您使用2.7支持的更新的语法,例如围绕异常,和/或更好的是,使用了从__future__导入的新功能, __future__代码转换为Python 3的时间要轻松得多(最多无需进行任何更改) 。 I'd suggest to follow this path first, for it can be trod gradually, without an abrupt jump to Python 3. 我建议先走这条路,因为它可以逐渐踩踏,而无需突然跳转到Python 3。

I suppose Python processes with different versions can interoperate, because object pickling format is compatible, and you can explicitly use a specific pickling protocol version on both sides to ensure that. 我想具有不同版本的Python进程可以互操作,因为对象酸洗格式是兼容的,并且您可以在两边显式使用特定的酸洗协议版本来确保这一点。 I don't think multiprocessing packages on either side would be too useful, though. 不过,我认为双方的multiprocessing程序包都不会有用。 Consider using eg ZeroMQ as a more general solution. 考虑使用例如ZeroMQ作为更通用的解决方案。

Unfortunately there is no "nice" or automatic way of handling the processing of 2.7 code under 3.5 (that works perfectly). 不幸的是,在3.5下没有完美的或自动的方式来处理2.7代码(效果很好)。

You mentioned that you are concerned about libraries, not your own code - firstly, you'd hope that if they are under active development, they will be updated. 您提到过,您担心的是库,而不是自己的代码-首先,希望它们如果正在积极开发中,则将对其进行更新。 If not, as you stated, then there is a possibility that they were written to be future proof. 如您所言,如果没有,则可能是为了将来证明而编写的。 I've found some good ones are (eg google-api-python-client, eg https://github.com/google/google-api-python-client/blob/master/setup.py ). 我发现一些不错的工具(例如google-api-python-client,例如https://github.com/google/google-api-python-client/blob/master/setup.py )。

Failing that, the only way to upgrade is to fix all the syntax changes yourself. 如果不这样做,升级的唯一方法就是自己修复所有语法更改。 Most common ones I deal with are around 'print' and exception handling. 我最常处理的是“打印”和异常处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM