我正在尝试使用execnet在Python3.8脚本中进行Python2.7函数调用

2024-05-29 11:34:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个用Python 3.8编写的脚本和一个用Python 2.7编写的函数。 该函数不能在Python3.8中传输,因为它使用专门为Python2.7设计的API。我已经搜索了几种解决方案,例如使用execnet和子流程,如本thread中所述

仔细实施上述线程中描述的最佳解决方案,即:

    import execnet

    def call_python_version(Version, Module, Function, ArgumentList):
        gw      = execnet.makegateway("popen//python=python%s" % Version)
        channel = gw.remote_exec("""
            from %s import %s as the_function
            channel.send(the_function(*channel.receive()))
        """ % (Module, Function))
        channel.send(ArgumentList)
        return channel.receive()

脚本尝试执行下一行代码时出错:

gw = execnet.makegateway("popen//python=python%s" % Version)

我得到的错误是:

FileNotFoundError: [WinError 2] The system cannot find the file specified

你知道是什么导致了这个错误吗?也欢迎通过3.X脚本运行Python2.7


Tags: the函数import脚本versionchannelfunction解决方案

热门问题