Is there a way to run python 2 code within a python 3 file? I have to use a function that is coded in python 2 and is located in a python 2 file. But i need to import it and use the function within a python 3 file. Is is possible to run that function is like a python 2 mode?
-
It depends on how your function looks like – yash Dec 03 '17 at 02:31
-
Is there a way to just run like one line as python 2? – chicken_wings Dec 03 '17 at 02:34
-
Nope afaik @chicken_wings – yash Dec 03 '17 at 02:35
-
Post the code of function that you are importing – yash Dec 03 '17 at 02:37
1 Answers
It is not possible to run Python 2 code with Python 3, at least not in general. Although converting by hand is fairly straight-forward.
If you have long files you should also consider using 2to3 which will apply the needed fixes to make your code run with Python 3.
If you already have Python 3 installed you simply have to run the following in your terminal.
2to3 your_file_name.py
Note that sometimes 2to3
will be unable to transpile from Python2 to Python3. If it notices it, it will give you warnings and indicate what lines you have to fix manually.
Although, it can also happen that 2to3
doesn't even notice the output code will not work. This is what happened in the example you gave me in the comments:
input('Type text here: ').encode('utf-8').encode('hex')
This will not work in Python3 for reasons that you can explore here.
The reason 2to3
doesn't realize it is because this is actually syntactically perfectly valid code. And actually you could foreshadow input
or str.encode
in a way that would make this working code.
In conclusion, sometime you have to read the error and fix the code yourself.

- 21,584
- 4
- 41
- 73
-
I did try using 2to3 but it only changed one line of code and didn't change the rest of the code, i can not post the code here as it is for an assignment and it will be checked for plagiarism – chicken_wings Dec 03 '17 at 03:29
-
Sometimes 2to3 cannot automatically fix your code. Did you get some warning after running it? Without the code or at least the warnings from 2to3, there is nothing we can do. – Olivier Melançon Dec 03 '17 at 03:32
-
-
-
Here is what I mean: Python 2 code: def test(): user_input = raw_input('Type text here:') hex_output = user_input.encode('utf-8').encode('hex') print (hex_output) test() Python 3 code by using 2to3: def test(): user_input = input('Type text here:') hex_output = user_input.encode('utf-8').encode('hex') print (hex_output) test() If you run the python 2 code it works but if you run it in python 3 it does not work. So what i want to do is call the python 2 file within the python 3 file without having to edit the python 2 code – chicken_wings Dec 03 '17 at 05:13
-
Again, you cannot run Python2 with Python3. It's like if you asked why Python is not able to compile C code. You just can't unless you have some transpiler (which 2to3 is). I found your bug though, 2to3 could indeed hardly figure it out. When you encode in utf-8 in Python3 it returns your a 'bytes' object which has no attribute encode. Here is the solution, but you will have to fix it manually: https://stackoverflow.com/questions/6624453/whats-the-correct-way-to-convert-bytes-to-a-hex-string-in-python-3 – Olivier Melançon Dec 03 '17 at 07:48