2

Previous Research:

I am running php version 5.6.6 and python version 3.4.3 on OS X.


Basically, the problem that I am running into is that if I run a python script via command line it works find but if I run it through a PHP script (using exec()) I get this error:

AttributeError: type object 'int' has no attribute 'from_bytes'

I have created and tested a miniature isolated test case to show the problem. I have already done a chmod 777 mypy.py to make sure mypy.py is executable.

myphp.php:

<?php

exec("/usr/bin/python mypy.py 1A", $output, $return);
var_dump($output);

mypy.py:

#!/usr/bin/env python

import string
import array
import binascii
import sys

if __name__ == "__main__":
    hexval = sys.argv[1]
    binval = binascii.unhexlify(hexval)
    binint = int.from_bytes(binval, byteorder='big', signed=False)
    print("int: " + str(binint))

(I know there are better ways to accomplish what is being done in this python script, I was just making a test case that would produce the same error)

When I run python mypy.py 1F via command line, I get this printed:

int: 31

But when I run php myphp.php via command line, I get this printed:

Traceback (most recent call last):
  File "mypy.py", line 11, in <module>
    binint = int.from_bytes(binval, byteorder='big', signed=False)
AttributeError: type object 'int' has no attribute 'from_bytes'
array(0) {
}

(Note: I have also executed whoami from the php script just to verify that my normal user is the one running the python script)

2
  • 1
    int has no method .from_bytes, that code would not run anywhere using python2, pretty sure you need /usr/bin/python3 Commented Mar 24, 2015 at 0:22
  • I just tried your test case. I'm get the same error that you are getting when running the .py script directly. I am running 2.7. Looks like int.from_bytes is >= 3.2? Commented Mar 24, 2015 at 0:23

2 Answers 2

3

int has no method .from_bytes in python2,/usr/bin/python uses the python 2 interpreter, you need to use /usr/bin/python3 to use your python3 interpreter.

exec("/usr/bin/python3 mypy.py 1A", $output, $return);

Making the file executable is also irrelevant as you are explicitly running it with python.

To run it as an executable and use the python3 interpreter specify the correct python version in your python shebang:

#!/usr/bin/env python3

Then:

exec("./mypy.py 1A", $output, $return);
Sign up to request clarification or add additional context in comments.

1 Comment

The problem was that I had a bash alias set up from /usr/local/bin/python3 to /usr/bin/python. So, indeed, when run with one version of python it had the error and when run with the other it did not have the error. Thank you for the solution of just putting the right python environment at the top of the file.
0

Sorry for previous edits, I misread the question. Are you sure that python in cli and /usr/bin/python resolve to the same binary? The behavior I see tells me PHP is trying to use Python 2.x (because int object doesn't have method from_bytes).

2 Comments

Thank you for your answer, you were indeed getting to the root of my problem. I have upvoted, but since Padraic's answer is more fully featured, I think I should select his as the right answer.
@StephenM347, this should be just a comment, but i can't post them yet. Thank you.

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.