I am observing a different output from a C++ compiled binary file, that calls some OpenCV libraries, executed via the python interpreter launched via manage.py ($ python2.7 manage.py shell) versus the standard python interpreter ($ python2.7). The output achieved from the bash shell is equivalent to that of the standard python interpreter.
It appears that something is different about the ‘environment’ of the python interpreter launched via manage.py as compared to the standard python shell. I’d like to know how to determine the difference between the two interpreters and ultimately how to have the result of the binary execution be the same.
Setup details:
- connected to web server via ssh (putty)
- Centos6-64bit
- /bin/bash
From within my Django project directory I run the following and the processed image (result of executing the binary file) is as I expect:
$ python2.7
Python 2.7.1 (r271:86832, Sep 13 2011, 19:13:17)
[GCC 4.4.4 20100726 (Red Hat 4.4.4-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> import os
>>> subprocess.call(['/home/username/engine/binary','/home/username/imagetmp/image.jpg'])
0
>>>
From within my Django project directory I run the following and the processed image is NOT as I expect:
$ python2.7 manage.py shell
Python 2.7.1 (r271:86832, Sep 13 2011, 19:13:17)
[GCC 4.4.4 20100726 (Red Hat 4.4.4-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import subprocess
>>> import os
>>> subprocess.call(['/home/username/engine/binary','/home/username/imagetmp/image.jpg'])
0
>>>
On the command line I run the following and the processed image is as I expect.
$ pwd
/home/username/engine/
$ ./binary /home/username/imagetmp/image.jpg
$
Within both python interpreters I’ve compared the following:
- sys.path (the result obtained from the python interpreter launched via manage.py has the path to my django project while the result from the standard python interpreter does not)
- os.environ (the result obtained from the python interpreter launched via manage.py includes the DJANGO_SETTINGS_MODULE and CELERY_LOADER enviroment variables while the result from the standard python interpreter does not)
- os.stat for every file in /home/username/engine and /home/username/engine/libs .. with no differences observed
-
I’ve also tried modifying the subprocess call which had no impact:
subprocess.call([‘/home/username/engine/binary’,’/home/username/imagetmp/image.jpg’], env=os.environ)
So the differences I’ve noted are:
- the (InteractiveConsole) line when the python interpreter shell is started via $ python2.7 manage.py shell .. I’m unsure of what this extra line means or rather if and what it’s presence implies and if it’s the cause of the difference I observe in behavior.
- the slight differences in results of sys.path and os.environ
My conclusion is that there is something fundamental that I’m unaware of regarding the differences between the python interpreter launched via manage.py versus the standard python interpreter. Any thoughts you have on how to debug this situation would be greatly appreciated.
The root cause of this observed issue was related to the way file paths were being handled in the binary. Once we realized this was the case and remedied the situation in the binary, we observed the correct behaviour i.e. execution in the python interpreter launched via manage.py led to the same result as execution in the standard python interpreter.