7

I am trying to activate my virtualenv using a php script or a python script but without using SSH.

This is to allow my website.com/something.py file to access certain libraries (if this can be done in another simpler way please let me know)

My PHP code is:

<?php
echo "A";
$result = exec("source ENV/bin/activate");

if ($result){
echo "Worked";
}
else{
echo "didnt work";
}

echo "B";
$result = system("python test.py");

?>

and I have test.py =

def main():
    print "hello"

    try:
        import xlrd
    except:
        try:
            print "xlrd didnt load"
            import MySQLdb
        except:
            print "mdb,xlrd didnt load"


main()

The virtualenv I have setup has xlrd installed. This is the output I get on the webpage:

Adidnt workBhello xlrd didnt load

It makes sense that xlrd didnt load but why is the source command not working? This all works in SSH

2
  • source is not an executable, but a shell built-in. Commented Mar 16, 2012 at 14:02
  • well, I also tried shell_exec("source ENV/bin/activate"); and it didnt work. Either way, what command can I run to get the expected result? Any idea? Commented Mar 16, 2012 at 14:05

1 Answer 1

11

According to the docs, sourcing the activate script inside a shell just tweaks the $PATH environment variable to point to the virtualenv's bin directory. This script can't work from PHP, because an external executable can never modify the caller's environment for security reasons.

The documentation also tells you what you can do instead:

If you directly run a script or the python interpreter from the virtualenv's bin/ directory (e.g. path/to/env/bin/pip or /path/to/env/bin/python script.py) there's no need for activation.

So you can just specify the full path to the Python installation instead:

$result = system("ENV/bin/python test.py");
Sign up to request clarification or add additional context in comments.

8 Comments

I tried :$result = system("ENV/bin/python test.py"); instead of $result = system("python test.py"); and Got :Adidnt workB When I tried it in SSH it says that ENV/bin/python isnt a file or directory but when I check it it exists
@sbeleidy: Obviously this is the same ENV than in your original code: source ENV/bin/activate. I can't know what the real path is, you have to figure that out yourself. What do you mean, you checked it and it exists?
You have this piece of code in your question: exec("source ENV/bin/activate"); How did you figure that path out?
I put command cd ENV then cd bin then dir and I see python but when I run the entire command it says file or directory doesnt exist
@sbeleidy: You know about the difference between absolute and relative paths, right? ENV is a relative path. Use the absolute path instead (probably something like /home/XYZ/ENV/bin/python).
|

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.