Reputation: 29
I have a python script written in Python 2.7, but want to import it in a python script written in 3.5, how do I make sure the python script knows what python version to use to read the script?
Thanks!
Upvotes: 0
Views: 67
Reputation: 11651
You can't simply import Python 2 code, unless it was specially written to be Python 2/3 compatible (which is not uncommon).
The usual recommendation would be to upgrade the Python 2 script to Python 3 if it can't be run by Python 3. The standard library 2to3
can help with this and there are third-party tools that can make this even easier, like python-future For a short script, I would second that recommendation.
However, this is not the only way.
A second option: you could run the Python 2 script under Python 2 inside a subprocess launched from Python 3. You can communicate between the processes using serialized data, like JSON or an older pickle protocol. This entails writing some glue code in Python 2 to help pass the data back and forth. If you just need a small interface to a large Python 2 program this may be easier than upgrading the whole thing to Python 3.
A third option is to use Tauthon (formerly known as "unofficial Python 2.8") for both scripts. Tauthon is completely compatible with Python 2 code, but adds a lot of Python 3 features you might want. This way you'll have to adapt your Python 3 code to Tauthon, instead of the Python 2 code to Python 3. Depending on the relative size and complexities of your two code bases, this may be the easier option, especially since Tauthon already supports most Python 3 features you might be using.
And finally, you can use a tool to automatically convert the script to Python 3 upon import, like lib2to3import. This is probably closest to what you were asking for, but I have not found such tools to be very reliable. This approach is not really any easier than just converting the script to Python 3, but if someone else is maintaining it, it might make upgrades easier, since you won't have to reconvert it yourself. Also, you can continue to use the same version of the Python 2 script from both Python 2 and Python 3 programs, even as you make changes, without the effort of making a 2/3 compatible script. Your mileage may vary.
Upvotes: 0
Reputation: 7908
If your python environment uses python 3.5, when you invoke a script, it will be run with the python version of the environment, i.e. 3.5.
If the script you are calling is not compatible with python 3.5, you might encounter errors or, even worse, unexpected results that might be more difficult to catch (cf. division below)
Common source of incompatibilities between python 2 and 3 are, but are not limited to:
print "hello"
(python 2.7) instead of print("hello")
(python 3),xrange
(python 2.7) instead of range
(python 3),9 / 4
yielding 2 (python 2.7) vs. yielding 2.25 (python 3).If you want your python 2.7 script to be compatible with python 3.5, you should make sure that it is compliant with python 3 standards using things like
from __future__ import print_function
from __future__ import division
from six.moves import range
For more information on making python 2 and 3 code work together, check out the documentation of Six: Python 2 and 3 Compatibility Library.
If you want to convert your python 2 script into python 3, you could use 2to3, but be careful.
Let's take this simple python 2.7 snippet:
# foo.py
for i in xrange(10):
print i
When calling 2to3 foo.py -w
, foo.py
becomes:
# foo.py
for i in range(10):
print(i)
If your goal is to use foo.py
with python 3 only from now on, 2to3
is a decent solution, though it does not resolve all the problems. However, if you wish to continue using your script with python 2.7 as well, this might be problematic. For instance, the use of range
in python 2.7 is highly inefficient when compared to xrange
.
Upvotes: 1