0

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.

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.

Writing a Python 2 script that uses nltk and then executing script that in my Python 3 script

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.

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.

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

Thanks

3
  • That depends on what kind of data you need to transfer between your scripts. But that's probably the best way to handle this for now. Commented Dec 29, 2012 at 0:46
  • I'm curious why you can't translate your script to py 2. Do you know about 3to2? Commented Dec 29, 2012 at 1:30
  • "nor can I convert my Python 3 file to Python 2." - Why not? Doing so it usually fairly trivial. Commented Dec 29, 2012 at 5:01

2 Answers 2

6

Is there any other way to do this?

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.)

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

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:

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"])

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.)

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.

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]])

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

Sign up to request clarification or add additional context in comments.

2 Comments

I tried py2output = subprocess.check_output(['python', "py2.py","-i a.txt"]) but it just prints out `-i a.txt" (my arguments).
@user1925847: What is the "it" there? You need to be a bit clearer. Also, do you actually want "-i a.txt" to be a single argument, rather than "-i" and "a.txt" as separate arguments?
1

The Anyone hear when NLTK 3.0 will be out? here in SO points out that...

There's a Python 3 branch:

https://github.com/nltk/nltk/tree/nltk-py3k

The answer is from July 2011. It could be improved since then.

I have just looked at https://github.com/nltk/nltk. There is at least the document that talks about Python 3 port related things https://github.com/nltk/nltk/blob/2and3/web/dev/python3porting.rst.

Here is a longer discussion on NLTK and Python 3 that you may be interested in.

And the Grants to Assist Kivy, NLTK in Porting to Python 3 (published 3 days ago) is directly related to the problem.

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.