3

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?

4
  • It depends on how your function looks like Commented Dec 3, 2017 at 2:31
  • Is there a way to just run like one line as python 2? Commented Dec 3, 2017 at 2:34
  • Nope afaik @chicken_wings Commented Dec 3, 2017 at 2:35
  • Post the code of function that you are importing Commented Dec 3, 2017 at 2:37

1 Answer 1

1

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.

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

6 Comments

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
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.
Also, if there is nothing to convert, it won't. right? @Olivier
I believe 2to3 should leave a python 3 script unchanged yes
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
|

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.