Activate virtualenv from python or a non-ssh script

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

This will allow my website.com/something.py file to access specific libraries (if you can do it in another easy way, 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 virtual machine I installed has xlrd installed. This is the result I get on the webpage:

 Adidnt workBhello xlrd didnt load 

It makes sense that xlrd did not load, but why does the source command not work? It all works in SSH

+4
source share
1 answer

According to docs , an activate script search inside the shell simply changes the $PATH environment variable to point to virtualenv bin . This script cannot work with PHP because an external executable can never change the caller’s environment for security reasons.

The documentation also tells you what you can do instead:

If you directly run a script or python interpreter from virtualenv bin/ (e.g. path/to/env/bin/pip or /path/to/env/bin/python script.py ) there is no need to activate.

That way, you can simply specify the full path to the Python installation:

 $result = system("ENV/bin/python test.py"); 
+5
source

Source: https://habr.com/ru/post/1401815/


All Articles