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)

Community
  • 1
  • 1
morsecoder
  • 1,479
  • 2
  • 15
  • 24
  • 1
    `int` has no method `.from_bytes`, that code would not run anywhere using python2, pretty sure you need `/usr/bin/python3` – Padraic Cunningham Mar 24 '15 at 00: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? – Darragh Enright Mar 24 '15 at 00:23

2 Answers2

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);
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • 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. – morsecoder Mar 24 '15 at 00:34
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).

the
  • 21,007
  • 11
  • 68
  • 101
Etki
  • 2,042
  • 2
  • 17
  • 40
  • 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. – morsecoder Mar 24 '15 at 00:31
  • @StephenM347, this should be just a comment, but i can't post them yet. Thank you. – Etki Mar 24 '15 at 00:34