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
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
sixmodule, the3to2tool, 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 simplest version is this:
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.pyas command-line arguments and/orstdin, passing data back as viastdout, 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 thesubprocessModule 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 frompy2.pytopy3.py, just havepy3.pydo this:And then in
py2.py, justprintwhatever you want to send back.