Getting python2.7 path in django app for subprocess call
Question: I am using linux. I am trying to run daemon from function in django views. I
want to run shell command from a view in Djangp app. I am using python 2.7.
Command needs python2.7 path.

My app will be like plug n play. So on system on which it is going to install
may have python installed on different location. So I want to make python path
dynamic.

Command will be

    
    
        usr/bin/python2.7 filename.py --start
    

On my system path is usr/bin/python2.7.

I found follwing using os.

On python shell I tried following code & I get what I want

    
    
        import os
        getPyPath  = os.popen('which python2.7', 'r')
        pyPath = getPyPath.read()
        pyPath.rstrip()
    

I got o/p as which is expected as below

> usr/bin/python2.7

So now how to get this code is django app view function & run it so that I can
get python path in a variable.

I found pythons subprocess module call using which we can run command through
shell using shell=True.

So can I get above code running in django view function using subprocess
call??

If not what is the other ways to get python path in variable in function
django views.

Thanks in advance.


Answer: To view the full path to the current Python interpreter, use `sys.executable`

    
    
    import sys
    
    print(sys.executable)
    

