在 Python 2 中使用 Python 3 [英] Using Python 3 with Python 2

查看:41
本文介绍了在 Python 2 中使用 Python 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Python 3 文件.想用网上的一个开源工具(nltk),可惜只支持Python 2.我没办法把它转换成Python 3,也不能把我的Python 3文件转换成Python 2.

I have a Python 3 file. I want to use an open-source tool on the internet (nltk), but unfortunately it only supports Python 2. There is no way for me to convert it to Python 3, nor can I convert my Python 3 file to Python 2.

如果用户没有给出某个参数(在 argparse 上),那么我会在我的文件中做一些事情.但是,如果用户确实给出了某个论点,则我需要使用 nltk.

If the user does not give a certain argument (on argparse) then I do something in my file. If the user does give a certain argument, however, I need to use nltk.

编写一个使用 nltk 的 Python 2 脚本,然后在我的 Python 3 脚本中执行该脚本

我目前的想法是用 Python 2 编写一个脚本,用 nltk 执行我想要的操作,然后从我当前的 Python 3 脚本运行它.但是,我实际上不知道如何做到这一点.我找到了这个代码:os.system(command),所以我将把它修改为 os.system("python py2.py")(其中 py2.py 是我新编写的 Python 2 文件).我不确定这是否有效.

My current idea is to write a script in Python 2 that does what I want with nltk and then run that from my current Python 3 script. However, I don't actually know how to do this. I found this code: os.system(command) and so I will modify it to be os.system("python py2.py") (where py2.py is my newly written Python 2 file). I'm not sure if that will work.

我也不知道这是否是解决我的问题的最有效方法.我在互联网上找不到任何关于它的信息.

I also don't know if that is the most efficient way to solve my problem. I cannot find any information about it on the internet.

传输的数据可能会非常大.目前,我的测试数据大约是 6600 行,utf-8.就我而言,功能比(在某种程度上)需要多长时间更重要.

The data transferred will probably be quite large. Currently, my test data is about 6600 lines, utf-8. Functionality is more important than how long it takes (to a certain extent) in my case.

另外,如何将值从 Python 2 脚本传递到 Python 3 脚本?

Also, how would I pass values from my Python 2 script to my Python 3 script?

谢谢

推荐答案

还有其他方法吗?

好吧,如果您确定无法将脚本转换为 Python 2,那么通过运行 Python 解释器让一个脚本调用另一个脚本可能是最好的 方式.(而且,这是 Python,最好的方法是,或者至少应该是唯一的方法.)

Well, if you're sure you can't convert your script to Python 2, then having one script call the other by running the Python interpreter probably is the best way. (And, this being Python, the best way is, or at least should be, the only way.)

但是你确定吗?在 six 模块之间,3to2 工具和 __future__ 语句,可能没有你想象的那么难.

But are you sure? Between the six module, the 3to2 tool, and __future__ statements, it may not be as hard as you think.

无论如何,如果您确实需要让一个脚本调用另一个脚本,您几乎不应该使用 os.system.正如该函数的 docs 所说:

Anyway, if you do need to have one script call the other, you should almost never use os.system. As the docs for that function say:

subprocess 模块为生成新进程和检索结果提供了更强大的工具;使用该模块比使用此功能更可取.请参阅子流程文档中的用子流程模块替换旧函数部分,了解一些有用的秘诀.

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes.

最简单的版本是这样的:

The simplest version is this:

subprocess.check_call(["python", "py2.py"])

这会运行您的脚本,等待它完成,并在脚本返回失败时引发异常——基本上,这是您想要对 os.system 执行的操作,但更好.(例如,它不会产生不必要的额外 shell,它会负责错误处理等.)

This runs your script, waits for it to finish, and raises an exception if the script returns failure—basically, what you wanted to do with os.system, but better. (For example, it doesn't spawn an unnecessary extra shell, it takes care of error handling, etc.)

这假设您需要共享的任何其他数据都以某种隐式的外部方式共享(例如,通过访问具有相同名称的文件).您最好将数据作为命令行参数和/或 stdin 传递给 py2.py,通过 stdout 将数据传回,或者甚至打开一个明确的管道或套接字来传递东西.在不了解您需要做什么的情况下,很难提出任何建议,但是文档,尤其是 subprocess 模块替换旧函数 有很多关于选项的讨论.

That assumes whatever other data you need to share is being shared in some implicit, external way (e.g., by accessing files with the same name). You might be better off passing data to py2.py as command-line arguments and/or stdin, passing data back as via stdout, or even opening an explicit pipe or socket to pass things over. Without knowing more about exactly what you need to do, it's hard to suggest anything, but the docs, especially the section Replacing Older Functions with the subprocess Module have lots of discussion on the options.

为了给你一个想法,这是一个简单的例子:将你的文件名参数之一传递给 py2.py,然后从 py2.py 取回数据到py3.py,让 py3.py 这样做:

To give you an idea, here's a simple example: to pass one of your filename arguments to py2.py, and then get data back from py2.py to py3.py, just have py3.py do this:

py2output = subprocess.check_output(["python", "py2.py", my_args[0]])

然后在py2.py中,只需print你想发回的任何内容.

And then in py2.py, just print whatever you want to send back.

这篇关于在 Python 2 中使用 Python 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆